gpt4 book ai didi

android - opendrawer() 在 drawerlayout 中抛出空指针错误

转载 作者:行者123 更新时间:2023-11-30 01:39:12 26 4
gpt4 key购买 nike

我是 Android 新手。
通过这个简单的程序,我想使用按钮 onclick() 打开一个抽屉。但是这给出了 null 异常错误。我通过谷歌搜索尝试了解决方案,但不幸的是没有解决它。
这是onCreate()方法

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

navListener()

           public void navListener(){


final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
ImageView nav = (ImageView) findViewById(R.id.navbarimagebutton);
nav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

mDrawerLayout.openDrawer(GravityCompat.START);
}
});



}

activity_main.xml

       <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<include layout="@layout/app_bar" />


</RelativeLayout>

DrawerLayout

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_height="match_parent"
android:layout_width="120dp"
xmlns:android="http://schemas.android.com/apk/res/android">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"></ListView>



</FrameLayout>
</android.support.v4.widget.DrawerLayout>

app_bar

        <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/red">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<ImageView
android:id="@+id/navbarimagebutton"
android:layout_width="0dp"
android:layout_weight=".25"
android:layout_height="fill_parent"
android:src="@drawable/menu"
/>


<View
android:id="@+id/homepagefirstview"

android:layout_width="10dp"
android:layout_height="fill_parent"></View>

<ImageView
android:layout_weight="1"
android:layout_width="0dp"
android:src="@drawable/hometitle"
android:layout_height="fill_parent" />
<View
android:layout_width="10dp"
android:layout_height="fill_parent"/>
<ImageView
android:layout_weight=".25"
android:layout_width="0dp"
android:src="@drawable/search"
android:layout_height="fill_parent" />

</LinearLayout>


</android.support.v7.widget.Toolbar>

最佳答案

你的问题是你要求你的 Activity setContentView(R.layout.activity_main);

并且这个文件不包含你的DrawerLayout

你需要做的:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<!-- add an id here since your using RelativeLayout -->
<include layout="@layout/app_bar" android:id="@+id/app_bar" />

<!-- include the drawer this way, probably need to add layout_below="@+id/app_bar" or whatever the id is for your app bar -->
<!-- Uncomment this <include layout="@layout/drawer_layout"/> -->

<!-- Or simply copy the drawer layout xml and add layout_below="@+id/app_bar" -->
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_below="@+id/app_bar"
android:layout_height="match_parent"
android:layout_width="match_parent">

<!-- main content area -->
<FrameLayout
android:id="@+id/content_area"
android:layout_height="match_parent"
android:layout_width="match_parent"/>

<!-- Slide out drawer section -->
<FrameLayout
android:layout_gravity="start"
android:layout_width="320dp"
android:layout_height="match_parent">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</FrameLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

您遇到了 NullPointerException,因为您的膨胀 View 不包含您正在引用的这个 DrawerLayout

关于android - opendrawer() 在 drawerlayout 中抛出空指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34728449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com