2015-03-25 76 views
0

我的應用程序有四個片段。當我第一次運行應用程序時,一切正常,但是當從其他片段跳回時,logcat會說「Nullpointerexception」。因爲我需要在兩個不同的片段使用地圖,所以我刪除了R.id.patrol_map_fragment地圖onDestroyView()片段上的java.lang.Nullpointerexception findView()

這是所有登錄

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" /> 
+0

把所有錯誤日誌 – ashutiwari4 2015-03-25 11:11:52

+2

post'findView();'方法 – nikis 2015-03-25 11:13:14

+0

什麼是行84? – 2015-03-25 11:13:38

回答

0

我不能評論,所以我會寫在這裏。

我在更新到com.android.support:appcompat-v7:22.0.0後得到了這個。

我不完全確定新的appcompat中發生了什麼變化,以實現此目的,但這是我所做的修復它。

我改變了我的

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

container.removeView(view); 

我也試過完全移除線和工作太。