在android上测试,片段[重复]上的NullPointerException

时间:2022-12-15 17:04:50

This question already has an answer here:

这个问题在这里已有答案:

Trying to implement some small test cases in my Android application, but at the moment i have some trouble getting the test class to find the fragment.

试图在我的Android应用程序中实现一些小的测试用例,但目前我在测试类找到片段时遇到了一些麻烦。

Here is my Activity that just create my fragment.

这是我的活动,只是创建我的片段。

public class DetailActivity extends FragmentActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_detail);
       Intent intent = getIntent();
       Bundle bundle = new Bundle();
       bundle.putParcelable("match", intent.getParcelableExtra("match"));
       DetailFragment fragment = new DetailFragment();
       fragment.setArguments(bundle);
       if (savedInstanceState == null) {
          getSupportFragmentManager().beginTransaction()
                  .add(R.id.container, fragment, "test")
                  .commit();
       }
   }
}

And my fragment looks like this.

我的片段看起来像这样。

public class DetailFragment extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

      context = getActivity().getApplicationContext();
      mh = getArguments().getParcelable("match");

      int id = context.getResources().getIdentifier(
              "c" + String.valueOf(mh.getChampionImage()),
              "drawable", context.getPackageName()
      );
    }

    @Override
    public void onStart() {
      super.onStart();
      if (Utility.hasNetworkConnection(context)) {
         FetchIcons fetchIcons = new FetchIcons();
         fetchIcons.execute(mh);
      }
    }

...//Some Code

My test looks like this.

我的测试看起来像这样。

public class DetailFragmentTest extends ActivityUnitTestCase<DetailActivity> {

   DetailActivity activity;
   DetailFragment fragment;

   public DetailFragmentTest() {
       super(DetailActivity.class);
   }

   @Override
   protected void setUp() throws Exception {
      super.setUp();
      Intent intent = new Intent(new MockContext(), DetailActivity.class);
      int[] i = {1, 2, 3, 4, 5, 6, 7};
      MatchHistory mh = new MatchHistory("Win", "23", "42", "31", "23414", "321", 22, true, i);
      intent.putExtra("match", mh);
      startActivity(intent, null, null);
      activity = getActivity();
      fragment = (DetailFragment) activity.getSupportFragmentManager().findFragmentByTag("test");

   }

   public void testText() {
    ...//Some more code
   }
}

Everything up to the part where i'm trying to receive the framgent works fine, but gives me NullPointerException as soon as i try to fetch it.

我尝试接收framgent的部分工作正常,但是一旦我尝试获取它就会给我NullPointerException。

And my stacktrace doesn't shed much light on the problem.

而我的堆栈跟踪并未对此问题有所了解。

java.lang.NullPointerException
at com.plushogskolan.chobi.matchhistory.DetailFragmentTest.setUp(DetailFragmentTest.java:34)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Been trying to solve it using several different methods, including just the ActivityInstrumentationTestCase2, but then i can't even get the Activity.

一直在尝试使用几种不同的方法来解决它,包括ActivityInstrumentationTestCase2,但后来我甚至无法获得Activity。

I'm clearly doing something wrong. But since not a single tutorial for unit testing in android have yielded any luck i can at least give it a shot here and see if anyone knows what the problem is.

我显然做错了什么。但是因为没有一个单独的Android测试教程产生任何运气,我至少可以在这里试一试,看看是否有人知道问题是什么。

Regards!

1 个解决方案

#1


0  

Fragments are attached lazily. Which means you will have to call "executePendingTransactions" like

片段是懒洋洋地附着的。这意味着您必须调用“executePendingTransactions”之类的

FragmentManager m = activity.getFragmentManager();
m.executePendingTransactions();

#1


0  

Fragments are attached lazily. Which means you will have to call "executePendingTransactions" like

片段是懒洋洋地附着的。这意味着您必须调用“executePendingTransactions”之类的

FragmentManager m = activity.getFragmentManager();
m.executePendingTransactions();