底部导航片段应用程序不断崩溃

时间:2019-07-16 12:49:22

标签: java android

我试图创建一个底部导航栏,但是当我尝试运行该应用程序时,它始终显示“导航栏已停止”。 我正在使用以下网站的代码:https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/

public class MainActivity extends AppCompatActivity {

    private ActionBar toolbar;

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

        toolbar = getSupportActionBar();

        toolbar.setTitle("Home");
        loadFragment(new HomeFragment());
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment;
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    toolbar.setTitle("Home");
                    fragment = new HomeFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.navigation_search:
                     toolbar.setTitle("Search");
                    fragment = new SearchFragment();
                    loadFragment(fragment);
                     return true;
                case R.id.navigation_category:
                     toolbar.setTitle("Category");
                    fragment = new CategoryFragment();
                    loadFragment(fragment);
                     return true;
                case R.id.navigation_share:
                     toolbar.setTitle("Share");
                    fragment = new ShareFragment();
                    loadFragment(fragment);
                     return true;
                case R.id.navigation_more_horiz:
                     toolbar.setTitle("More");
                    fragment = new MoreFragment();
                    loadFragment(fragment);
                     return true;
            }
            return false;
        }
    };

    private void loadFragment(Fragment fragment) {
        // load fragment
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_container, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
}

}

我需要做什么

这是logcat,但是我很新,不知道该怎么办:

2019-07-16 20:57:32.007 7235-7235/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 7235
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.RuntimeException: com.example.myapplication.MainActivity@51ea29f must implement OnFragmentInteractionListener
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.RuntimeException: com.example.myapplication.MainActivity@51ea29f must implement OnFragmentInteractionListener
    at com.example.myapplication.HomeFragment.onAttach(HomeFragment.java:83)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1404)
    at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195)
    at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078)
    at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
    at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
    at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
    at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
    at android.app.Activity.performStart(Activity.java:7029)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

1 个答案:

答案 0 :(得分:0)

在MainActivity中,您必须实现接口

G:\Software\ML>pip list
Package         Version
--------------- ----------
cycler          0.10.0
joblib          0.13.2
kiwisolver      1.1.0
matplotlib      3.1.1
numpy           1.16.4+mkl
pandas          0.25.0rc0
pip             19.1.1
pyparsing       2.4.0
python-dateutil 2.8.0
pytz            2019.1
scikit-learn    0.21.2
scipy           1.3.0
setuptools      40.8.0
six             1.12.0

G:\Software\ML>python
>>> import sklearn
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\__init__.py", line 76, in <module>
from .base import clone
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\base.py", line 16, in <module>
from .utils import _IS_32BIT
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\utils\__init__.py", line 17, in <module>
from . import _joblib
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\utils\_joblib.py", line 8, in <module>
import joblib
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\joblib\__init__.py", line 119, in <module>
from .parallel import Parallel
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\joblib\parallel.py", line 28, in <module>
from ._parallel_backends import (FallbackToBackend, MultiprocessingBackend,
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\joblib\_parallel_backends.py", line 22, in <module>
from .executor import get_memmapping_executor
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\joblib\executor.py", line 14, in <module>
from .externals.loky.reusable_executor import get_reusable_executor
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\joblib\externals\loky\__init__.py", line 12, in <module>
from .backend.reduction import set_loky_pickler
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\joblib\externals\loky\backend\reduction.py", line 125, in <module>
from joblib.externals import cloudpickle  # noqa: F401
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\joblib\externals\cloudpickle\__init__.py", line 3, in <module>
from .cloudpickle import *
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\joblib\externals\cloudpickle\cloudpickle.py", line 152, in <module>
_cell_set_template_code = _make_cell_set_template_code()
File "C:\Users\akarasin\AppData\Local\Programs\Python\Python38\lib\site-packages\joblib\externals\cloudpickle\cloudpickle.py", line 133, in _make_cell_set_template_code
return types.CodeType(
TypeError: an integer is required (got type bytes)
>>>

}