2017-08-25 92 views
0

所以有BottomNavigationView當我點擊任何項目時,應該打開一個片段。但最終得到以下錯誤 登錄:切換片段時發生錯誤

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.GapWorker.remove(android.support.v7.widget.RecyclerView) on a null object reference 
at android.support.v7.widget.RecyclerView.onDetachedFromWindow(RecyclerView.java:2534) 
at android.view.View.dispatchDetachedFromWindow(View.java:14392) 
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3108) 
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3105) 
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3105) 
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3105) 
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3105) 
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3105) 
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3105) 
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3105) 
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3105) 
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4472) 
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4445) 
at android.view.ViewGroup.removeView(ViewGroup.java:4377) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1411)at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) 
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:753) 
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363) 
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149) 
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013) 
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5835) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

片段

public class ProfileFragment extends Fragment { 



    public ProfileFragment() { 
     // Required empty public constructor 
    } 

    /** 
    * Use this factory method to create a new instance of 
    * this fragment using the provided parameters. 
    * 
    * @param param1 Parameter 1. 
    * @param param2 Parameter 2. 
    * @return A new instance of fragment ProfileFragment. 
    */ 
    // TODO: Rename and change types and number of parameters 
    public static ProfileFragment newInstance(String param1, String param2) { 
     ProfileFragment fragment = new ProfileFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

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

     return view; 
    } 

切換完成後爲:

runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     transaction.replace(R.id.main_container_home, currFragment,currTag); 
     transaction.commit(); 

     } 
}); 

任何幫助將有益

解決方案的嘗試:

1.清空碎片佈局,只設置與空的子相對佈局。錯誤。

應用程序在片段的onCreateView()中崩潰。

更新:終於得到崩潰的根本原因是因爲我加入視圖在容器即container.add(視圖)

回答

0

這並不是片段之間進行切換的方式。您必須將NavigationBottomMenu放入Activity中,創建與BottomNav中的項目數量一樣多的片段,然後從那裏提交片段。

這是MainActivity.java

public class MainActivity extends AppCompatActivity{ 

private BottomNavigationView bottomNavigationView; 
private SearchPropertyFragment searchPropertyFragment; 
private ChatFragment chatFragment; 
private ProfileFragment profileFragment; 
private NotificationFragment notificationFragment; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    searchPropertyFragment = new SearchPropertyFragment(); 
    chatFragment = new ChatFragment(); 
    profileFragment = new ProfileFragment(); 
    notificationFragment = new NotificationFragment(); 

    getSupportFragmentManager().beginTransaction().add(R.id.fragment_frame, searchPropertyFragment).commit(); 

    bottomNavigationView = (BottomNavigationMenu) findViewById(R.id.bottom_navigation); 

    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
      int id = item.getItemId(); 
      switch (id){ 
       case R.id.bottom_menu_properties: 
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, searchPropertyFragment).commit(); 
        break; 
       case R.id.bottom_menu_chat: 
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, chatFragment).commit(); 
        break; 
       case R.id.bottom_menu_profile: 
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, profileFragment).commit(); 
        break; 
       case R.id.bottom_menu_notifications: 
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, notificationFragment).commit(); 
        break; 
      } 
      return true; 
     } 
    }); 
} 

}

這是activity_main.xml中

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout 
     android:id="@+id/fragment_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/bottom_navigation" /> 

    <android.support.design.widget.BottomNavigationView 
     android:id="@+id/bottom_navigation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="@color/navigationItemBackground" 
     app:itemIconTint="@color/navigationItemIcon" 
     app:itemTextColor="@color/navigationItemtext" 
     app:menu="@menu/bottom_navigation_items" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

相應地定義你的片段。

希望這有助於。

+0

我正在以同樣的方式做。但使用runOnUiThread,我也刪除,但仍然得到相同的錯誤。 –

+0

不需要在單獨的線程上執行操作。它只能在UI線程中完成 –

+0

只需刪除線程並在bottomNav中執行setOnNavigationItemSelectedListener() –