gpt4 book ai didi

java - 解决 Android getActivity() 中的 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 05:59:08 24 4
gpt4 key购买 nike

我正在尝试通过 Eclipse 为我的 Android 应用程序运行 JUnit 测试,但我不断收到 NullPointerExceptions,似乎是因为 getActivity() 返回 null。

我该如何解决这个问题?这是我的类(class):

package app.test.views;

import net.app.R;
import app.views.AddUserActivity;
import app.views.CategoryActivity;
import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.widget.EditText;

public class AddUserActivityTest2 extends ActivityUnitTestCase<AddUserActivity> {
private Intent startIntent;

public AddUserActivity myActivity;
private EditText usernameBox;

public AddUserActivityTest2() {
super(AddUserActivity.class);
}

@Override
public void setUp() throws Exception {
super.setUp();
myActivity = getActivity();
usernameBox = (EditText) myActivity.findViewById(R.id.inputUsername);
startIntent = new Intent(Intent.ACTION_MAIN);
}

public void testCat0() { assertTrue(0==0); }
}

这是我在 JUnit 测试中收到的错误。

java.lang.NullPointerException
at app.test.views.AddUserActivityTest2.setUp(AddUserActivityTest2.java:37)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

这是 logcat 的重要部分:

----- begin exception -----
04-02 13:15:36.845: I/TestRunner(2091): java.lang.NullPointerException
04-02 13:15:36.845: I/TestRunner(2091): at app.test.views.AddUserActivityTest2.setUp(AddUserActivityTest2.java:37)
04-02 13:15:36.845: I/TestRunner(2091): at junit.framework.TestCase.runBare(TestCase.java:132)
04-02 13:15:36.845: I/TestRunner(2091): at junit.framework.TestResult$1.protect(TestResult.java:115)
04-02 13:15:36.845: I/TestRunner(2091): at junit.framework.TestResult.runProtected(TestResult.java:133)
04-02 13:15:36.845: I/TestRunner(2091): at junit.framework.TestResult.run(TestResult.java:118)
04-02 13:15:36.845: I/TestRunner(2091): at junit.framework.TestCase.run(TestCase.java:124)
04-02 13:15:36.845: I/TestRunner(2091): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
04-02 13:15:36.845: I/TestRunner(2091): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
04-02 13:15:36.845: I/TestRunner(2091): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
04-02 13:15:36.845: I/TestRunner(2091): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
04-02 13:15:36.845: I/TestRunner(2091): ----- end exception -----

最佳答案

在执行getActivity()之前尝试运行startActivity(new Intent(), null, null);,以便创建 Activity 。

关于java - 解决 Android getActivity() 中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22818032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com