Android Fragment生命周期问题(onActivityResult上的NullPointerException) [英] Android Fragment lifecycle issue (NullPointerException on onActivityResult)

查看:492
本文介绍了Android Fragment生命周期问题(onActivityResult上的NullPointerException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,因为我找不到任何解释。
我有一个使用TabManager显示片段的FragmentActivity,如下所示:

  public class WorkOrderFormTabFragmentActivity extends FragmentActivity {
TabHost mTab​​Host;
TabManager mTab​​Manager;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.work_order_form_tab_new);
mTab​​Host =(TabHost)findViewById(android.R.id.tabhost);
mTab​​Host.setup();

mTab​​Manager =新的TabManager(this,mTabHost,R.id.realtabcontent);

mTab​​Manager.addTab(mTabHost.newTabSpec(form).setIndicator(Form),
WorkOrderFormFragment.class,null);
mTab​​Manager.addTab(mTabHost.newTabSpec(pictures).setIndicator(Pictures),
PictureListFragment.class,null);

if(savedInstanceState!= null){
mTab​​Host.setCurrentTabByTag(savedInstanceState.getString(tab));



@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putString(tab,mTabHost.getCurrentTabTag());
}

public static class TabManager implements TabHost.OnTabChangeListener {
private final FragmentActivity mActivity;
private final TabHost mTab​​Host;
private final int mContainerId;
private final HashMap< String,TabInfo> mTabs = new HashMap< String,TabInfo>();
TabInfo mLastTab;

static final class TabInfo {
private final String tag;
private final Class<?> CLSS;
private final Bundle args;
private片段片段;

TabInfo(String _tag,Class<> _class,Bundle _args){
tag = _tag;
clss = _class;
args = _args;



static class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;

public DummyTabFactory(Context context){
mContext = context;
}

@Override
public View createTabContent(String tag){
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;


$ b $ public TabManager(FragmentActivity activity,TabHost tabHost,int containerId){
mActivity = activity;
mTab​​Host = tabHost;
mContainerId = containerId;
mTab​​Host.setOnTabChangedListener(this);
}

public void addTab(TabHost.TabSpec tabSpec,Class<?> clss,Bundle args){
tabSpec.setContent(new DummyTabFactory(mActivity));
String tag = tabSpec.getTag();

TabInfo info = new TabInfo(tag,clss,args);

info.fragment = mActivity.getSupportFragmentManager()。findFragmentByTag(tag);
if(info.fragment!= null&&!info.fragment.isDetached()){
FragmentTransaction ft = mActivity.getSupportFragmentManager()。beginTransaction();
ft.detach(info.fragment);
ft.commit();
}

mTab​​s.put(tag,info);
mTab​​Host.addTab(tabSpec);
}

@Override
public void onTabChanged(String tabId){
TabInfo newTab = mTab​​s.get(tabId);
if(mLastTab!= newTab){
FragmentTransaction ft = mActivity.getSupportFragmentManager()。beginTransaction();
if(mLastTab!= null){
if(mLastTab.fragment!= null){
//ft.detach(mLastTab.fragment);
ft.hide(mLastTab.fragment); (newTab!= null){
if(newTab.fragment == null){
newTab.fragment = Fragment.instantiate(mActivity,

}
if
newTab.clss.getName(),newTab.args);
ft.add(mContainerId,newTab.fragment,newTab.tag);
} else {
//ft.attach(newTab.fragment);
ft.show(newTab.fragment);
}
}

mLastTab = newTab;
ft.commit();
mActivity.getSupportFragmentManager()。executePendingTransactions();





$ b

在这个的第二个标签中FragmentActivity用户可以管理图片列表并使用相机添加更多图片。

片段代码:

  public class PictureListFragment extends Fragment {

static final int TAKE_PICTURE_ACTIVITY = 1;
static final int EDIT_PICTURE_ACTIVITY = 2;

FormPictureListAdapter lvAdapter;

@Override
public查看onCreateView(LayoutInflater inflater,ViewGroup viewgrp,
Bundled savedInstanceState){
//膨胀此片段的布局
View cont = inflater.inflate(R.layout.form_picture_list,viewgrp,false);

LinearLayout容器=(LinearLayout)cont.findViewById(R.id.formPictureListLayout);
try {

final Context context = getActivity();
ListView ls2 =新的ListView(上下文);
//在LV
中清除以前的结果ls2.setAdapter(null);
//填充
ArrayList< MFPicture> pictures = new ArrayList< MFPicture>();
//pictures.add(0,new MFPicture()); $()()()()()()()()getApplication())。getCurrentForm()。getPictures());
pictures.addAll(((MFApplication)getActivity

lvAdapter = new FormPictureListAdapter(context,pictures);
ls2.setAdapter(lvAdapter);
LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,0,1f);
ls2.setLayoutParams(Params);
ls2.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view,
int position,long id){
final MFPicture picture = ((MFPictureView)view).getPicture();
final int idx = position;
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener(){
@Override $ b $ public void onClick (DialogInterface dialog,int which){
switch(which){
case DialogInterface.BUTTON_POSITIVE:
//编辑图片
EditPictureActivity.setPicture(picture);
意图configurationOpen = new Intent(getActivity(),EditPictureActivity.class);
startActivityForResult(configurationOpen,EDIT_PICTURE_ACTIVITY);
打破;

case DialogInterface.BUTTON_NEGATIVE:
//删除图片
((MFApplication)getActivity()。getApplication())。getCurrentForm()。getPictures()。remove(idx) ;
MFUtils.deleteFile(picture.getPath());
lvAdapter.updatePictureList(((MFApplication)getActivity()。getApplication())。getCurrentForm()。getPictures());
lvAdapter.notifyDataSetChanged();
休息;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getResources()。getString(R.string.wo_bem_regie_list_el_action))。setPositiveButton(getResources()。getString(R.string.wo_bem_regie_list_el_edit),dialogClickListener)
.setNegativeButton(getResources()。 getString(R.string.wo_bem_regie_list_el_delete),dialogClickListener).show();
}
});
container.addView(ls2);

LayoutInflater layoutInflater =(LayoutInflater)getActivity()。getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.add_btn_bottom,container);
view.setBackgroundResource(R.drawable.list_selector_even);
TextView text =(TextView)view.findViewById(R.id.title);
text.setText(getResources()。getString(R.string.wo_picturelist_add));
view.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
v.setBackgroundResource(R.drawable.list_selector_even);
String pictureFile =((MFApplication)getActivity()。getApplication())。getNextPictureFile();
String picPath = MFUtils.MF_STORAGE_PATH +/+ pictureFile;
Log.e(FormPictureListActivity, :+ picPath);
//setBackgroundResource (android.R.drawable.list_selector_background);
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent。 putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(picPath)));
startActivityForResult(intent,TAKE_PICTURE_ACTIVITY);
} catch(ActivityNotFoundException e){
Log.e(FormPictureListActivity ,呼叫失败, E);
}
}
});
}
catch(Exception e){
e.printStackTrace();
Log.e(FormPictureListActivity,Error:,e);
}

return cont;
}

@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
//super.onActivityResult(requestCode,resultCode,data);
String pictureFile =((MFApplication)getActivity()。getApplication())。getPictureFile();
Log.d(FormPictureListActivity,ActivityResult:+ resultCode);
Log.d(FormPictureListActivity,ActivityResult-picFile:+ pictureFile);
if(requestCode == TAKE_PICTURE_ACTIVITY){
if(resultCode == getActivity()。RESULT_OK){
Log.d(FormPictureListActivity,ActivityResult:OK);
MFPicture picture = new MFPicture(MFPicture.TYPE_PICTURE,MFUtils.MF_STORAGE_PATH +/+ pictureFile);
((MFApplication)getActivity()。getApplication())。getCurrentForm()。getPictures()。add(picture);
lvAdapter.updatePictureList(((MFApplication)getActivity()。getApplication())。getCurrentForm()。getPictures());
lvAdapter.notifyDataSetChanged();
EditPictureActivity.setPicture(picture);
Intent configurationOpen = new Intent(getActivity(),EditPictureActivity.class);
startActivityForResult(configurationOpen,EDIT_PICTURE_ACTIVITY);


else if(requestCode == EDIT_PICTURE_ACTIVITY){
EditPictureActivity.getPicture()。setComment(EditPictureActivity.getPicture()。getCommentUIValue());
lvAdapter.updatePictureList(((MFApplication)getActivity()。getApplication())。getCurrentForm()。getPictures());
lvAdapter.notifyDataSetChanged();



code
$ b

在我的测试中一切正常设备(Nexus 5,Galaxy Nexus,Galaxy Mini 2),但我时常收到我无法访问的其他设备(主要是运行Android 4.0.4的设备)的错误:

  java.lang.RuntimeException:无法恢复活动

{com.mf.mobile.android/com.mf.mobile.android.WorkOrderFormTabFragmentActivity}: java.lang.RuntimeException:将结果resultInfo {who = null,request = 131073,result = -1,data = null}传递给activity {com.mf.mobile.android/com.mf.mobile.android.WorkOrderFormTabFragmentActivity}失败: java.lang.NullPointerException $ b $在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
在android。 android.app.ActivityThread.access
(ActivityThread.java:135)$ android
在android中
(ActivityThread.java:135)
at android.app.ActivityThread.access $ 600 .app.ActivityThread $ H.handleMessage(ActivityThread.java:1248)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java :137)
在android.app.ActivityThread.main(ActivityThread.java:4645)
在java.lang.reflect.Method.invokeNative(本地方法)
在java.lang.reflect .Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit .main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
引起:java.lang.RuntimeException:提交结果失败ResultInfo {who = null,request = 131073 ,result = -1,data = null} to activity {com.mf.mobile.android/com.mf.mobile.android.WorkOrderFormTabFragmentActivity}:java.lang.NullPointerException
at android.app.ActivityThread.deliverResults( ActivityThread.java:3156)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2599)
... 12 more
Ca用于:java.lang.NullPointerException
at com.timewise.mobile.android.fragments.PictureListFragment.onActivityResult(PictureListFragment.java:138)
at android.support.v4.app.FragmentActivity.onActivityResult( FragmentActivity.java:166)
at android.app.Activity.dispatchActivityResult(Activity.java:4662)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3152)
.. 。13 more

NPE出现在这行代码中:
lvAdapter.updatePictureList(( (MframeApplication)getActivity()。getApplication())。getCurrentForm()。getPictures());

这意味着此时lvAdapter变量为null。但是这个变量应该已经在片段的onCreateView中设置了......这让我觉得,在某些时候,片段可能已经被重新创建,而无需调用onCreateView。



我找不到关于这个问题的任何解释。你可以帮我解决这个问题吗?



非常感谢

解决方案

好吧,我认为如果用户打开你的应用程序然后点击主页按钮,然后点击显示正在运行的应用程序按钮(位于主页按钮右侧的按钮)或按住某个主页按钮,就会发生这种情况手机并选择你的应用程序回到它。然后,如果你看看生命周期,它会调用onResume(这是上面的堆栈跟踪),而无需再次调用onCreateView。

//developer.android.com/reference/android/app/Fragment.html#Lifecyclerel =nofollow>片段生命周期



现在,你应该可以通过进入Nexus 5的开发人员选项并选择不要保留活动来模拟此情况以查看它。然后打开你的应用程序,去那个片段点击主页按钮,然后运行应用程序按钮,并选择你的应用程序,我认为它会显示你的例外。


I get an issue which for I cannot find any explanation. I have a FragmentActivity that displays fragments using a TabManager, as follows :

public class WorkOrderFormTabFragmentActivity extends FragmentActivity {
TabHost mTabHost;
TabManager mTabManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.work_order_form_tab_new);
    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);

    mTabManager.addTab(mTabHost.newTabSpec("form").setIndicator("Form"),
            WorkOrderFormFragment.class, null);
    mTabManager.addTab(mTabHost.newTabSpec("pictures").setIndicator("Pictures"),
            PictureListFragment.class, null);

    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("tab", mTabHost.getCurrentTabTag());
}

public static class TabManager implements TabHost.OnTabChangeListener {
    private final FragmentActivity mActivity;
    private final TabHost mTabHost;
    private final int mContainerId;
    private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
    TabInfo mLastTab;

    static final class TabInfo {
        private final String tag;
        private final Class<?> clss;
        private final Bundle args;
        private Fragment fragment;

        TabInfo(String _tag, Class<?> _class, Bundle _args) {
            tag = _tag;
            clss = _class;
            args = _args;
        }
    }

    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context mContext;

        public DummyTabFactory(Context context) {
            mContext = context;
        }

        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
        mActivity = activity;
        mTabHost = tabHost;
        mContainerId = containerId;
        mTabHost.setOnTabChangedListener(this);
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
        tabSpec.setContent(new DummyTabFactory(mActivity));
        String tag = tabSpec.getTag();

        TabInfo info = new TabInfo(tag, clss, args);

        info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);
        if (info.fragment != null && !info.fragment.isDetached()) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
            ft.detach(info.fragment);
            ft.commit();
        }

        mTabs.put(tag, info);
        mTabHost.addTab(tabSpec);
    }

    @Override
    public void onTabChanged(String tabId) {
        TabInfo newTab = mTabs.get(tabId);
        if (mLastTab != newTab) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                    //ft.detach(mLastTab.fragment);
                    ft.hide(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(mActivity,
                            newTab.clss.getName(), newTab.args);
                    ft.add(mContainerId, newTab.fragment, newTab.tag);
                } else {
                    //ft.attach(newTab.fragment);
                    ft.show(newTab.fragment);
                }
            }

            mLastTab = newTab;
            ft.commit();
            mActivity.getSupportFragmentManager().executePendingTransactions();
        }
    }
}

In the second tab of this FragmentActivity user can manage a list of pictures and use the camera to add more pictures.

Fragment code :

public class PictureListFragment extends Fragment {

static final int TAKE_PICTURE_ACTIVITY = 1;
static final int EDIT_PICTURE_ACTIVITY = 2;

FormPictureListAdapter lvAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewgrp,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View cont = inflater.inflate(R.layout.form_picture_list, viewgrp, false);

    LinearLayout container = (LinearLayout)cont.findViewById(R.id.formPictureListLayout);
    try{

        final Context context = getActivity();
        ListView ls2 = new ListView(context);
        // clear previous results in the LV
        ls2.setAdapter(null);
        // populate
        ArrayList<MFPicture> pictures = new ArrayList<MFPicture>();
        //pictures.add(0, new MFPicture());
        pictures.addAll(((MFApplication)getActivity().getApplication()).getCurrentForm().getPictures());

        lvAdapter = new FormPictureListAdapter(context, pictures);
        ls2.setAdapter(lvAdapter);
        LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 0, 1f);
        ls2.setLayoutParams(Params);
        ls2.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
              final MFPicture picture = ((MFPictureView)view).getPicture();
              final int idx = position;
              DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      switch (which){
                      case DialogInterface.BUTTON_POSITIVE:
                          //Edit picture
                          EditPictureActivity.setPicture(picture);
                          Intent configurationOpen = new Intent(getActivity(), EditPictureActivity.class);
                          startActivityForResult(configurationOpen, EDIT_PICTURE_ACTIVITY);
                          break;

                      case DialogInterface.BUTTON_NEGATIVE:
                          //Delete picture
                          ((MFApplication)getActivity().getApplication()).getCurrentForm().getPictures().remove(idx);  
                          MFUtils.deleteFile(picture.getPath());
                          lvAdapter.updatePictureList(((MFApplication)getActivity().getApplication()).getCurrentForm().getPictures());
                          lvAdapter.notifyDataSetChanged();
                          break;
                      }
                  }
              };
              AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
              builder.setMessage(getResources().getString(R.string.wo_bem_regie_list_el_action)).setPositiveButton(getResources().getString(R.string.wo_bem_regie_list_el_edit), dialogClickListener)
                    .setNegativeButton(getResources().getString(R.string.wo_bem_regie_list_el_delete), dialogClickListener).show();
          }
         });
        container.addView(ls2);

        LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=layoutInflater.inflate(R.layout.add_btn_bottom,container);
        view.setBackgroundResource(R.drawable.list_selector_even);
        TextView text = (TextView)view.findViewById(R.id.title);
        text.setText(getResources().getString(R.string.wo_picturelist_add));
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v){
                v.setBackgroundResource(R.drawable.list_selector_even);
                String pictureFile = ((MFApplication)getActivity().getApplication()).getNextPictureFile();
                String picPath = MFUtils.MF_STORAGE_PATH+"/"+pictureFile;
                Log.e("FormPictureListActivity", "PicturePath : "+picPath);
                //setBackgroundResource(android.R.drawable.list_selector_background);
                try {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(picPath)));
                    startActivityForResult(intent, TAKE_PICTURE_ACTIVITY);
                } catch (ActivityNotFoundException e) {
                    Log.e("FormPictureListActivity", "Call failed", e);
                }
            }
      });
    }
    catch(Exception e){
      e.printStackTrace();
      Log.e("FormPictureListActivity", "Error:", e);
    }

    return cont;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);
    String pictureFile = ((MFApplication)getActivity().getApplication()).getPictureFile();
    Log.d("FormPictureListActivity", "ActivityResult:"+resultCode);
    Log.d("FormPictureListActivity", "ActivityResult-picFile:"+pictureFile);
    if (requestCode == TAKE_PICTURE_ACTIVITY){
        if(resultCode == getActivity().RESULT_OK){
            Log.d("FormPictureListActivity", "ActivityResult:OK");
            MFPicture picture = new MFPicture(MFPicture.TYPE_PICTURE, MFUtils.MF_STORAGE_PATH+"/"+pictureFile);
            ((MFApplication)getActivity().getApplication()).getCurrentForm().getPictures().add(picture);
            lvAdapter.updatePictureList(((MFApplication)getActivity().getApplication()).getCurrentForm().getPictures());
            lvAdapter.notifyDataSetChanged();
            EditPictureActivity.setPicture(picture);
            Intent configurationOpen = new Intent(getActivity(), EditPictureActivity.class);
            startActivityForResult(configurationOpen, EDIT_PICTURE_ACTIVITY);
        }
    }
    else if (requestCode == EDIT_PICTURE_ACTIVITY){
        EditPictureActivity.getPicture().setComment(EditPictureActivity.getPicture().getCommentUIValue());
        lvAdapter.updatePictureList(((MFApplication)getActivity().getApplication()).getCurrentForm().getPictures());
        lvAdapter.notifyDataSetChanged();
    }
}
}

Everything works well on my test devices (Nexus 5, Galaxy Nexus, Galaxy Mini 2) but I receive from time to time errors from other devices I have no access to (mainly devices running Android 4.0.4) :

java.lang.RuntimeException: Unable to resume activity 

{com.mf.mobile.android/com.mf.mobile.android.WorkOrderFormTabFragmentActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131073, result=-1, data=null} to activity {com.mf.mobile.android/com.mf.mobile.android.WorkOrderFormTabFragmentActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
at android.app.ActivityThread.access$600(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1248)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4645)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131073, result=-1, data=null} to activity {com.mf.mobile.android/com.mf.mobile.android.WorkOrderFormTabFragmentActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3156)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2599)
... 12 more
Caused by: java.lang.NullPointerException
at com.timewise.mobile.android.fragments.PictureListFragment.onActivityResult(PictureListFragment.java:138)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
at android.app.Activity.dispatchActivityResult(Activity.java:4662)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3152)
... 13 more

The NPE appears on this line of code : lvAdapter.updatePictureList(((MframeApplication)getActivity().getApplication()).getCurrentForm().getPictures());

This means that at this time lvAdapter variable is null. But this variable should have been set in onCreateView of Fragment... Which makes me think that, at some point Fragment may have been recreated without calling onCreateView.

I cannot find any explanation about this issue. Can you help me on this one?

Many thanks

解决方案

Ok, I think this can happen if the user has your app open and then hits the home button, then either hits the show running applications button (the one that is to the right of the home button) or holds down the home button on some phones and selects your app to go back to it. Then, if you look at the lifecycle it will call onResume (which is what is in the stack trace above) without calling onCreateView again.

Fragment Lifecycle

Now, you should be able to mimic this to see it happening by going into the Developer options on your Nexus 5, and selecting Don't keep activities. Then open your app, go to that fragment hit the home button, then the running apps button and select your app and I think it will show you that exception.

这篇关于Android Fragment生命周期问题(onActivityResult上的NullPointerException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆