Java 获取报告不必要的强制转换表达式MainActivity.onCreate

Java 获取报告不必要的强制转换表达式MainActivity.onCreate,java,android,android-fragments,Java,Android,Android Fragments,public类MainActivity扩展了AppCompatActivity{ ArrayList items=新建ArrayList(); 适配器; 列表视图列表视图; 私人抽屉布局和抽屉布局; 私人行动律师事务所; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); NDRAWRLAYOUT=(抽屉布局)

public类MainActivity扩展了AppCompatActivity{
ArrayList items=新建ArrayList();
适配器;
列表视图列表视图;
私人抽屉布局和抽屉布局;
私人行动律师事务所;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NDRAWRLAYOUT=(抽屉布局)findViewById(R.id.DrawerLayout);
ntogle=newactionBarDrawerToggle(this,ndrawrLayout,R.string.open,R.string.close);
nDrawerLayout.addDrawerListener(nToggle);
nToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<!--    tools:context=".MainActivity">-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

   <View
       android:layout_width="match_parent"
       android:layout_height="2dp"
       android:background="#ca1515"
       />

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

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu"
    android:layout_gravity="start">
</android.support.design.widget.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

我正在尝试添加片段菜单,但运行后出现了此错误 生成时没有出现错误

为什么我(抽屉布局)未使用

报告不必要的强制转换表达式


我尝试添加>导入com.your.package.R

有一个类似的问题,并给出了解释-

解决方案

在您的代码中,无需强制转换findViewByIdAPI的结果-请参见下文

nDrawerLayout = findViewById(R.id.drawerlayout);
请将您的问题作为文本而不是屏幕截图提供完整的堆栈跟踪。
nDrawerLayout = findViewById(R.id.drawerlayout);