片段findView()上的java.lang.Nullpointerexception

时间:2015-03-25 11:10:36

标签: java android android-fragments nullpointerexception

我的应用程序有四个片段。当我第一次运行应用程序时,一切正常,但是当从其他片段跳回时,logcat会说“Nullpointerexception”。因为我需要在两个不同的片段中使用地图,所以我会在R.id.patrol_map_fragment删除 onDestroyView() 地图。

这都是日志

    E/AndroidRuntime(5541): FATAL EXCEPTION: main
E/AndroidRuntime(5541): java.lang.NullPointerException
E/AndroidRuntime(5541):     at com.mt.fragment.PatrolFragment.findView(PatrolFragment.java:84)
E/AndroidRuntime(5541):     at com.mt.fragment.PatrolFragment.onCreateView(PatrolFragment.java:69)
E/AndroidRuntime(5541):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
E/AndroidRuntime(5541):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
E/AndroidRuntime(5541):     at android.app.BackStackRecord.run(BackStackRecord.java:635)
E/AndroidRuntime(5541):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1404)
E/AndroidRuntime(5541):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
E/AndroidRuntime(5541):     at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(5541):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(5541):     at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(5541):     at android.app.ActivityThread.main(ActivityThread.java:4914)
E/AndroidRuntime(5541):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(5541):     at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(5541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
E/AndroidRuntime(5541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
E/AndroidRuntime(5541):     at dalvik.system.NativeStart.main(Native Method)

PatrolFrgament.java:

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

        if (mBaseView != null) {
            ViewGroup parent = (ViewGroup) mBaseView.getParent();
            if (parent != null)
                parent.removeView(mBaseView);
        }
        try {
            mBaseView= inflater.inflate(R.layout.fragment_patrol, container, false);
        } catch (InflateException e) {
        }

        findView();
        init();
        return mBaseView;
    }

    private void findView() {
        // TODO Auto-generated method stub

        FragmentManager manager = getActivity().getFragmentManager();
        Fragment f = manager.findFragmentById(R.id.map_fragment);
        View view = f.getView();
        mTitleBarView = (TitleBarView) view.findViewById(R.id.title_bar);
        btn_patrol_camera = (Button) mTitleBarView
                .findViewById(R.id.title_btn_right);

        //*** This is the ERROR line ***/           
        minfoView = (View) mBaseView.findViewById(R.id.patrol_currinfo);
            rl_patrol_type = (RelativeLayout) mBaseView
                    .findViewById(R.id.rl_patrol_type);
            rl_patrolInfo_Normal = (RelativeLayout) minfoView
                    .findViewById(R.id.trl11);
            rl_patrolInfo_Warning = (RelativeLayout) minfoView
                    .findViewById(R.id.trl12);
            rl_patrolInfo_NoTax = (RelativeLayout) minfoView
                    .findViewById(R.id.trl21);
            rl_patrolInfo_NoCert = (RelativeLayout) minfoView
                    .findViewById(R.id.trl22);

            spinner = (Spinner) mBaseView
                    .findViewById(R.id.Spinner_patrol_mapRadius);
        }

          ... ...

        @Override
        public void onDestroyView() {
            // TODO Auto-generated method stub
            super.onDestroyView();
            Map f = (Map) getFragmentManager().findFragmentById(R.id.patrol_map_fragment);
            if (f != null)
                getFragmentManager().beginTransaction().remove(f).commit();
    }

这是我的 fragment_patrol.xml 布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical"
    android:background="@color/whites">

    <RelativeLayout
        android:id="@+id/rl_patrol_map"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5" >

        <fragment
            android:id="@+id/map_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.mt.patrol.Map" />
    </RelativeLayout>

... ...


   <include
            android:id="@+id/patrol_currinfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ll_patrol_devide"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            layout="@layout/patrol_currinfo" />
</LinearLayout>

--- 更新 --- 我试图在onCreateView()删除try.catch {}块,直接写下:

mBaseView = inflater.inflate(R.layout.fragment_patrol, container,false);

但是错误是二进制XML文件行#15:错误膨胀类片段,就是布局XML文件中的错误。

<fragment
            android:id="@+id/map_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.mt.patrol.Map" />

1 个答案:

答案 0 :(得分:0)

我无法发表评论,所以我会在这里写一下。

我在更新到com.android.support:appcompat-v7:22.0.0之后得到了这个。

我不完全确定新appcompat中发生了什么变化才能实现这一目标,但这就是我为解决这个问题而采取的措施。

我改变了我的

((ViewGroup) mBaseView).removeView(mBaseView);

container.removeView(view);

我也尝试完全删除该行,这也有效。