恢复setActionBarTitle问题时,应用程序崩溃

时间:2016-02-16 07:22:57

标签: android fragment toolbar resume

我的应用程序会在我恢复时崩溃,如果它已经在后台说15到20分钟。罪魁祸首是我的Viewfragment中的这一行((MainActivity) getActivity()).setActionBarTitle("Stories");

该行在我的MainActivity上引用了这个

public void setActionBarTitle(String title) {
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle(title);
    }

这是应用程序的外观......

MainActivity->> ViewFragment->> Fragment1 OR Fragment2。

每当我从Fragment1中的Another Activity恢复应用程序时,然后按回去Fragment1然后再返回ViewFragment它崩溃指向上面的行..Log如下:

我最好的猜测是,MainActivity被长期杀死,因此无法启动......我怎么能解决这个问题?

java.lang.RuntimeException: 
Unable to start activity ComponentInfo{com.reelforge.utility/com.reelforge.utility.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
 at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
 at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
 at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.reelforge.utility.MainActivity.setActionBarTitle(MainActivity.java:155)
 at com.reelforge.utility.Viewfragment.ViewFragment.onCreate(ViewFragment.java:32)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1939)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1029)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:2037)
at android.support.v4.app.FragmentController.dispatchCreate(FragmentController.java:154)
 at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:289)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:61)
 at br.liveo.navigationliveo.NavigationLiveo.onCreate(NavigationLiveo.java:112)
at android.app.Activity.performCreate(Activity.java:5231)

2 个答案:

答案 0 :(得分:1)

使用界面设置MainActivity的标题,例如。

public interface ToolbarListener {
    void setTitle(String title);
}

在你的MainActiviy中实现这个

@Override
public void setTitle(String title) {
    if (getSupportActionBar() != null && !TextUtils.isEmpty(title)) {
        getSupportActionBar().setTitle(title);
    }
}

最后在像你这样的片段的OnAttach()中调用它

@Override
public void onAttach(Activity context) {
    super.onAttach(context);
    if (context instanceof ToolbarListener) {
        ((ToolbarListener) context).setTitle("My Title");
    }
}

答案 1 :(得分:0)

您应该使用接口在片段和活动之间进行交互(http://developer.android.com/intl/es/training/basics/fragments/communicating.html)。另外一个预防措施,就在((MainActivity) getActivity()).setActionBarTitle("Stories");之前你应该检查重写是as-

if(isVisible()) {
((MainActivity) getActivity()).setActionBarTitle("Stories");
}

在片段的简历中。