Sunday, April 28, 2013

[android help] NullPointerException with fragments


My application contains ViewPager in MainActivity with two fragments. First fragment contains ListView. I never see this, but I see reports from users about RuntimeException in Google Play:



java.lang.RuntimeException: Unable to resume activity {com.my.project/com.my.project.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.my.project/com.my.project.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.my.project/com.my.project.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2107)
... 12 more
Caused by: java.lang.NullPointerException
at com.my.project.FirstFragment.load(FirstFragment.java:551)
at com.my.project.MainActivity.onActivityResult(MainActivity.java:132)
at android.app.Activity.dispatchActivityResult(Activity.java:3908)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
... 13 more


Here is code from FirstFragment.java:



public class FirstFragment extends Fragment {

ArrayList listItems = null;
ArrayAdapter mAdapter;

void load() {
mAdapter.clear(); // I get error here, that is FirstFragment.java:551
...
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

listItems = new ArrayList();
mAdapter = new MyAdapter(getActivity(), listItems);
listView.setAdapter(mAdapter);
}

}


That is all occuerences of mAdapter. And I can't understand, how it can cause NullPointerException. I think that sometimes Fragment1 destroying and recreating, but without onCreateView(). I talk with one of users which have this problem and he say me, that this bug run away after he uncheck "Don't keep activities" in "Developer options" in settings. I don't know, can I fix it? Or I need to say all my users "uncheck this option"?


In my NainActivity:



public class MainActivity extends FragmentActivity {

Vector fragments;
ViewPager viewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

viewPager = (ViewPager) findViewById(R.id.pager);

fragments = new Vector();
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));

viewPager.setOffscreenPageLimit(fragments.size());
PagerAdapter pagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);

viewPager.setAdapter(pagerAdapter);

viewPager.setCurrentItem(0);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==1) {
((MainChat)fragments.get(0)).load();
}
}

class PagerAdapter extends FragmentPagerAdapter implements IconPagerAdapter {

private List fragments;

public PagerAdapter(FragmentManager fm, List fragments) {
super(fm);
this.fragments = fragments;
}

@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}

@Override
public int getCount() {
return this.fragments.size();
}

}

}


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...