返回Fragment时出现NullPointerException

时间:2014-02-28 20:53:02

标签: android android-fragments nullpointerexception android-fragmentactivity

我用来处理活动,当Android杀死他们时,我会用来照顾我的活动(onSaveStateInstance()onRestoreInstanceState()

现在我正在使用Fragments,当我从背景回到Fragment时,有时(有几个)得到NullPointerException。我无法得到日志,因为它发生在我上火车或在街上行走时。

我该怎么办?我该如何处理onSaveStateInstance()?我有一个FragmentActivity,其中有4个标签,我添加到hashMap

我已经阅读了StackOverflow中的帖子,但我无法理解,因为他们要求在旋转屏幕或其他内容时重新创建片段。

提前致谢。


修改

  

03-01 11:58:52.410:E / AndroidRuntime(31948):致命异常:主要   03-01 11:58:52.410:E / AndroidRuntime(31948):java.lang.RuntimeException:无法启动活动ComponentInfo {creact.placard / creact.placard.MainActivity}:java.lang.NullPointerException   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.app.ActivityThread.access $ 700(ActivityThread.java:134)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1217)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.os.Handler.dispatchMessage(Handler.java:99)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.os.Looper.loop(Looper.java:137)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.app.ActivityThread.main(ActivityThread.java:4856)   03-01 11:58:52.410:E / AndroidRuntime(31948):at java.lang.reflect.Method.invokeNative(Native Method)   03-01 11:58:52.410:E / AndroidRuntime(31948):at java.lang.reflect.Method.invoke(Method.java:511)   03-01 11:58:52.410:E / AndroidRuntime(31948):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1007)   03-01 11:58:52.410:E / AndroidRuntime(31948):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)   03-01 11:58:52.410:E / AndroidRuntime(31948):at dalvik.system.NativeStart.main(Native Method)   03-01 11:58:52.410:E / AndroidRuntime(31948):引起:java.lang.NullPointerException   03-01 11:58:52.410:E / AndroidRuntime(31948):at creact.placard.MapFragment.onCreateView(MapFragment.java:59)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.app.Activity.performStart(Activity.java:5057)   03-01 11:58:52.410:E / AndroidRuntime(31948):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2054)   03-01 11:58:52.410:E / AndroidRuntime(31948):... 11更多

然后它继续做2或3个HttpGet,然后停止。

在该行上我开始定义我的按钮或图像视图:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    context = getActivity();
    if (container == null) {
        return null;
    }
    if (layout != null) {
            ViewGroup parent = (ViewGroup) layout.getParent();
            if (parent != null)
                parent.removeView(layout);
     }
     try {
         layout = inflater.inflate(R.layout.mappa, container, false);
     } catch (Exception e) {
         e.printStackTrace();
     }
     mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

     mMap.getUiSettings().setZoomControlsEnabled(true);
     negoziTab = (ImageView)layout.findViewById(R.id.negoziTab); // LINE 59 WHEN CAUSES ERROR
     eventiTab = (ImageView)layout.findViewById(R.id.eventiTab);
     promoTab = (ImageView)layout.findViewById(R.id.promoTab);
     tuttiTab = (ImageView)layout.findViewById(R.id.tuttiim);

这是第51行TRY CATCH上的ERROR LOG:

  

03-01 12:15:50.919:W / System.err(2849):android.view.InflateException:二进制XML文件行#86:错误扩充类片段   03-01 12:15:50.929:W / System.err(2849):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)   03-01 12:15:50.929:W / System.err(2849):在android.view.LayoutInflater.rInflate(LayoutInflater.java:746)   03-01 12:15:50.929:W / System.err(2849):在android.view.LayoutInflater.inflate(LayoutInflater.java:489)   03-01 12:15:50.929:W / System.err(2849):在android.view.LayoutInflater.inflate(LayoutInflater.java:396)   03-01 12:15:50.929:W / System.err(2849):at creact.placard.MapFragment.onCreateView(MapFragment.java:51)   03-01 12:15:50.929:W / System.err(2849):在android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)   03-01 12:15:50.929:W / System.err(2849):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)   03-01 12:15:50.929:W / System.err(2849):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)   03-01 12:15:50.929:W / System.err(2849):在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)   03-01 12:15:50.929:W / System.err(2849):在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)   03-01 12:15:50.929:W / System.err(2849):在android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)   03-01 12:15:50.929:W / System.err(2849):在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)   03-01 12:15:50.929:W / System.err(2849):在android.app.Activity.performStart(Activity.java:5057)   03-01 12:15:50.929:W / System.err(2849):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2054)   03-01 12:15:50.929:W / System.err(2849):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)   03-01 12:15:50.929:W / System.err(2849):在android.app.ActivityThread.access $ 700(ActivityThread.java:134)   03-01 12:15:50.929:W / System.err(2849):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1217)   03-01 12:15:50.929:W / System.err(2849):在android.os.Handler.dispatchMessage(Handler.java:99)   03-01 12:15:50.929:W / System.err(2849):在android.os.Looper.loop(Looper.java:137)   03-01 12:15:50.929:W / System.err(2849):在android.app.ActivityThread.main(ActivityThread.java:4856)   03-01 12:15:50.929:W / System.err(2849):at java.lang.reflect.Method.invokeNative(Native Method)   03-01 12:15:50.929:W / System.err(2849):at java.lang.reflect.Method.invoke(Method.java:511)   03-01 12:15:50.929:W / System.err(2849):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1007)   03-01 12:15:50.939:W / System.err(2849):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)   03-01 12:15:50.939:W / System.err(2849):at dalvik.system.NativeStart.main(Native Method)   03-01 12:15:50.939:W / System.err(2849):引起:java.lang.IllegalArgumentException:二进制XML文件行#86:重复id 0x7f060057,标记null或父id 0x0与com的另一个片段.google.android.gms.maps.SupportMapFragment   03-01 12:15:50.939:W / System.err(2849):在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)   03-01 12:15:50.939:W / System.err(2849):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)   03-01 12:15:50.939:W / System.err(2849):... 24更多

它是SupportMapFragment里面的内容。

再次感谢。



绝对编辑

我的问题在于我的布局在膨胀时。正在发生的是着名的

问题

Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

正如编辑所说:

  

编辑:存在一些不利影响,例如退出应用程序并再次启动时。由于应用程序不一定完全关闭(但只是在后台进入休眠状态),因此我提交的先前代码在重新启动应用程序时将失败。我已经将代码更新为适合我的内容,包括进入&退出地图并退出并重新启动应用程序,我对try-catch位不太满意,但它似乎运行得很好。

简要说明:当我尝试充气时,我不能因为这个问题,但我的布局是空的

我怎么能处理它?<​​/ p>

3 个答案:

答案 0 :(得分:0)

在我看来,你打电话给getActivity()。在它之前放一个空指针检查:

//some stuff

if(getActivity()!=null){
    //...
}

//other stuff

答案 1 :(得分:0)

我认为问题在于你在onCreateView()中使用了MapFragment的活动。

您应该将此部分代码移至onActivityCreated(),因为在活动的onCreate()方法返回时会调用它。

如果要在onCreate()中初始化MapFragment,它可能尚未创建,并且在调用它时它将返回null。

您可以尝试如下。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
   super.onActivityCreated(savedInstanceState);

   mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

   mMap.getUiSettings().setZoomControlsEnabled(true);

}

答案 2 :(得分:0)

if(yourfragment !==null)
{
   getActivity().getFragmentManager().beginTransaction().remove(yourfragment).commit();
                                    getFragmentManager().popBackStack();

  }
}

onBackPressed中使用此功能。它解决了我的问题。