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(視圖)
我正在以同樣的方式做。但使用runOnUiThread,我也刪除,但仍然得到相同的錯誤。 –
不需要在單獨的線程上執行操作。它只能在UI線程中完成 –
只需刪除線程並在bottomNav中執行setOnNavigationItemSelectedListener() –