gpt4 book ai didi

java - 选择双倍 BottomNavigation 项目时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 00:19:16 24 4
gpt4 key购买 nike

我正在尝试使用 BottomNavigationView 创建一个应用程序。我添加了三个 fragment 它只工作一次但是当我尝试从 botton View 应用程序崩溃中重新选择一个项目时。这是我的 mainActivity.java 代码

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

private TextView mTextMessage;

FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;


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

BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);

fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();

//fragmentTransaction.add(R.id.fragment, homeFragment).commit();


}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {


switch (item.getItemId()) {
case R.id.navigation_home:
fragmentTransaction.replace(R.id.fragment, new HomeFragment()).commit();
return true;
case R.id.navigation_dashboard:
fragmentTransaction.replace(R.id.fragment, new Dashboardfragment()).commit();
return true;
case R.id.navigation_notifications:
fragmentTransaction.replace(R.id.fragment, new NotificationFragment()).commit();
return true;
}
return false;
}


}

我的一个 fragment .java 代码如下。在此之前,我遇到了一个不同的问题,我无法替换我的 fragment 意味着即使我尝试更改,我的主 fragment 仍然存在。

public class HomeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
if (container != null) {
container.removeAllViews();
}

View view = inflater.inflate(R.layout.fragment_home,container,false);

return view;
}
}

好的,这是我的日志文件:

10-07 02:38:05.768 22895-22964/com.example.anonymous.bottomnavigationbar I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3.C8.05.01.00.115.128_msm8916_64_refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.3.C8.05.01.00.115.128__release_AU (I55c48cad9a)
OpenGL ES Shader Compiler Version: E031.25.03.04
Build Date: 03/21/17 Tue
Local Branch:
Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.3.C8.05.01.00.115.128
Local Patches: NONE
Reconstruct Branch: NOTHING
10-07 02:38:05.788 22895-22964/com.example.anonymous.bottomnavigationbar I/OpenGLRenderer: Initialized EGL, version 1.4
10-07 02:38:05.848 22895-22964/com.example.anonymous.bottomnavigationbar D/OpenGLRenderer: Enabling debug mode 0
10-07 02:38:05.928 22895-22895/com.example.anonymous.bottomnavigationbar W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
10-07 02:38:12.918 22895-22895/com.example.anonymous.bottomnavigationbar D/AndroidRuntime: Shutting down VM
10-07 02:38:12.918 22895-22895/com.example.anonymous.bottomnavigationbar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.anonymous.bottomnavigationbar, PID: 22895
java.lang.IllegalStateException: commit already called
at android.app.BackStackRecord.commitInternal(BackStackRecord.java:714)
at android.app.BackStackRecord.commit(BackStackRecord.java:705)
at com.example.anonymous.bottomnavigationbar.MainActivity.onNavigationItemSelected(MainActivity.java:44)
at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:821)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:968)
at android.support.design.internal.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:95)
at android.view.View.performClick(View.java:4781)
at android.view.View$PerformClick.run(View.java:19907)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:160)
at android.app.ActivityThread.main(ActivityThread.java:5541)
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:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
10-07 02:38:12.968 22895-22895/com.example.anonymous.bottomnavigationbar I/Process: Sending signal. PID: 22895 SIG: 9
10-07 02:38:12.968 22895-22895/com.example.anonymous.bottomnavigationbar V/Process: killProcess [22895] Callers=com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:99 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 <bottom of call stack>

最佳答案

现在我解决了这个错误,只需创建一个方法并在那里创建和初始化 FragmentManager 和 FragmentTrasaction,它对我来说非常有用。可能是错误是由于我们只能为多次提交提交 FragmentTransaction 一次,我们需要一次又一次地声明它,顺便说一句。这是我的方法:

private void openFragment(final Fragment fragment)   {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment, fragment);
transaction.addToBackStack(null);
transaction.commit();

}

关于java - 选择双倍 BottomNavigation 项目时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46613848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com