D
D
DeNissss44442020-11-13 12:26:24
Web development
DeNissss4444, 2020-11-13 12:26:24

Why does the application crash when connecting Toolbar to MainActivity?

Now minSdkVersion 19, as I understand it, it did not pull the Toolbar. When I connect the Toolbar to MainActivity, the application crashes when I start it, if I comment it out, then everything starts. Tell me what can be done so that with the same minimum version of the SDK, the Toolbar would work for me.

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            toolbar = (Toolbar)findViewById(R.id.main_toolbar);
        }
        // setSupportActionBar(toolbar);
      //  getSupportActionBar().setTitle("Chatden");

        viewPager = (ViewPager) findViewById(R.id.main_tabs_pager);
        tabsAccessorAdapter = new TabsAccessorAdapter(getSupportFragmentManager());
        viewPager.setAdapter(tabsAccessorAdapter);

        tabLayout = (TabLayout) findViewById(R.id.mainTabLayout);
        tabLayout.setupWithViewPager(viewPager);

    }

Here are the logs
11-13 11:19:58.057 16347-16347/com.example.chatden E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.chatden, PID: 16347
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chatden/com.example.chatden.MainActivity}: java.lang.ClassCastException: androidx.appcompat.widget.Toolbar cannot be cast to android.widget.Toolbar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2511)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2571)
        at android.app.ActivityThread.access$900(ActivityThread.java:165)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1409)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:171)
        at android.app.ActivityThread.main(ActivityThread.java:5620)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
     Caused by: java.lang.ClassCastException: androidx.appcompat.widget.Toolbar cannot be cast to android.widget.Toolbar
        at com.example.chatden.MainActivity.onCreate(MainActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:6320)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1121)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2571) 
        at android.app.ActivityThread.access$900(ActivityThread.java:165) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1409) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:171) 
        at android.app.ActivityThread.main(ActivityThread.java:5620) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2020-11-13
@DeNissss4444

That's why. Imports need to be fixed.
5fae5770cf51f372405256.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question