Androidx Appcompat错误setSupportActionBar((Toolbar)findViewById(R.id.toolbar));

时间:2019-12-18 02:03:17

标签: java android androidx

我很难解决这个问题,有人可以帮我吗?我错过了什么?错误setSupportActionBar((Toolbar)findViewById(R.id.toolbar));

我的活动

public class MainActivityCustonlistViewnew extends AppCompatActivity {

   private ArrayList<UserInfo> userInfos;
   private CustomListAdapter customListAdapter;
   private ListView customListView;
   private String[] names={
        "3 Khan",
        "2 Ahmed",
        "1 Iqbal"
   };
   private String[] professions={
        "Rank 1",
        "Rank 2",
        "Rank 3"
   };
   private int[] photos={
        R.drawable.sample_5,
        R.drawable.sample_1,
        R.drawable.sample_6
   };

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

        //Here is the problem
        setSupportActionBar((Toolbar)findViewById(R.id.toolbar));

        customListView=(ListView)findViewById(R.id.custom_list_view);
        userInfos=new ArrayList<>();
        Arrays.sort(names, String.CASE_INSENSITIVE_ORDER);
        customListAdapter=new CustomListAdapter(userInfos,this);
        customListView.setAdapter(customListAdapter);
        getDatas();
        customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //Toast.makeText(MainActivityCustonlistViewnew.this, "Name : " + names[i] + "\n Profession : " + professions[i], Toast.LENGTH_SHORT).show();  
            }
        });
    }

我的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".MainActivityCustonlistViewnew">

    <include android:id="@+id/toolbar" layout="@layout/custom_toolbar_layout"/>    

    <ListView
       android:id="@+id/custom_list_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
</LinearLayout>

我的进口

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toolbar;

import java.util.ArrayList;
import java.util.Arrays;

import androidx.appcompat.app.AppCompatActivity;

MyError :我不明白,工具栏一定有问题吗?,我使用的是androidx且导入已完成,仍然出现错误。

    Caused by: java.lang.ClassCastException: androidx.appcompat.widget.Toolbar cannot be cast to android.widget.Toolbar
    at com.seamans.marina.MainActivityCustonlistViewnew.onCreate(MainActivityCustonlistViewnew.java:47)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) ```

0 个答案:

没有答案