因此,当我点击任何项目时,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()崩溃。
UpDate:最终得到崩溃的根本原因是因为我在容器中添加了视图,即container.add(view)
答案 0 :(得分:0)
这不是在片段之间切换的方法。你必须将NavigationBottomMenu放在Activity中,创建与BottomNav中的项目数一样多的片段,然后从那里提交片段。
这是 MainActivity.java
t2.start();
t2.join();
t1.start();
}
这是 activity_main.xml
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;
}
});
}
相应地定义你的片段。
希望这有助于。