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

时间:2017-10-06 20:59:37

标签: java android fragment bottomnavigationview

我正在尝试使用BottomNavigationView创建应用。我添加了三个片段,它只能工作一次,但是当我尝试从botton视图中重新选择一个项目应用程序崩溃。这是我的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;
    }


}

和我的一个片段.java代码如下所示。在我遇到一个不同的问题,我无法替换我的片段意味着我的家庭片段仍然存在,即使我试图改变。

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;
    }
}

ok这是我的日志文件:

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> 

2 个答案:

答案 0 :(得分:1)

好了,现在我只是通过创建一个方法并在那里创建和初始化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();

    }

答案 1 :(得分:0)

每次点击都需要一个全新的交易,但您只需在onCreate()开始一次,并多次尝试commit()。您可以按如下方式更新代码以解决问题:

// Remove following two lines...
// 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);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    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;
}