![](/img/trans.png)
[英]java.lang.NullPointerException: on a null object reference
[英]Unit testing. java.lang.NullPointerException
每個人! 我正在為移動應用程序編寫單元測試。 但是我有一個錯誤,我不知道如何解決。 該錯誤具有以下形式:
java.lang.NullPointerException
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:841)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
at com.example.projectMP3.SplashScreenActivityTest.testLaunchOfNewActivity(SplashScreenActivityTest.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
程序代碼“SplashScreenActivity.java” :
public class SplashScreenActivity extends AppCompatActivity {
LinearLayout linearLayout;
/** Toolbar */
androidx.appcompat.widget.Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
toolbar = findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
linearLayout = findViewById(R.id.Splash_Activity);
linearLayout.setOnClickListener(v -> {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
finish();
});
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
);
Objects.requireNonNull(getSupportActionBar()).hide();
Thread thread = new Thread() {
public void run() {
try {
sleep(3000);
} catch (Exception e) {
e.printStackTrace();
} finally {
Intent intent = new Intent(SplashScreenActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
}
};
thread.start();
}
}
程序代碼“SplashScreenActivityTest.java” :
import android.widget.TextView;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
public class SplashScreenActivityTest {
@Test
public void testLaunchOfNewActivity() {
SplashScreenActivity activity = new SplashScreenActivity();
TextView nameApplication = activity.findViewById(R.id.textView3);
assertNotNull(nameApplication.getText());
}
}
程序代碼“activity_splash_screen.xml” :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Splash_Activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:orientation="vertical"
tools:context=".SplashScreenActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@drawable/tab_indicater"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="320dp"
android:layout_height="320dp"
android:layout_gravity="center_horizontal"
app:srcCompat="@drawable/splash_screen_logo"
android:layout_marginTop="100dp"
app:tint="#120a8f"
android:contentDescription="@string/startSplashScreen" />
</LinearLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:gravity="center"
android:text="@string/appName"
android:textColor="#FFAB00"
android:textSize="50sp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:layout_marginTop="2dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/all_rights_are_reserved"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
這是我第一次寫單元測試。 我希望能得到你的幫助! 先感謝您。
與您的Activity
類相關的問題。 您只是嘗試像一個簡單的類一樣測試活動。 但是在android中, Activity
是系統管理的系統組件,不能只使用構造函數來創建Activity
。
在官方網站上有一個如何測試活動的文檔: https ://developer.android.com/guide/components/activities/testing
您似乎正在調用該活動並訪問該活動中的 UI 組件。 我不知道你是否可以在不啟動應用程序的情況下在單元測試中做到這一點,這使它成為儀器測試(應該用 AndroidJunit4 測試)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.