Java 按下包含的布局零件时出现NullPointerException
霍兹特 我有一个包含listview的布局中包含的静态标题。该功能工作得很好,但是每当我点击作业时,它就会抛出一个NullPointerException(我不希望任何事情发生) 不知道为什么会这样 Container.xmlJava 按下包含的布局零件时出现NullPointerException,java,android,android-layout,Java,Android,Android Layout,霍兹特 我有一个包含listview的布局中包含的静态标题。该功能工作得很好,但是每当我点击作业时,它就会抛出一个NullPointerException(我不希望任何事情发生) 不知道为什么会这样 Container.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:lay
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<include android:id="@+id/headerItem"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="76dip"
android:background="@color/standard_background"
layout="@layout/List_Item"/>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/headerItem"
android:background="@color/standard_background" />
</RelativeLayout>
DrawerLayout.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The navigation drawer -->
<LinearLayout
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start" >
<ListView android:id="@+id/drawer_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice"
android:divider="@color/light_gray"
android:dividerHeight="0.1dp"
android:background="@color/standard_background"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/drawer_content_padding"
/>
</FrameLayout>
您正在xml中分配一个新id:
<include android:id="@+id/headerItem" // remove it
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="76dip"
android:background="@color/standard_background"
layout="@layout/List_Item"/>
应该是修复NPE。问题在于抽屉布局 抽屉宽度设置为
match_parent
,从而使其覆盖一些其他视图,并导致在按任意位置(空格中的任意位置)时发生异常
当我将宽度更改为与我的
内容框架的左边框的值相同时,一切都很完美。match\u家长完成了这项工作,但随后它放松了抽屉布局的本质(
上面的问题确实是因为在框架布局中添加了边距,您正在使用这些边距制作片段。这很奇怪,但却是真的
尝试删除页边距,您的布局将如下所示:
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
类似的情况
让我们知道这是否有帮助。:)您有可以与我们共享的堆栈跟踪吗?删除xmlns:android=”http://schemas.android.com/apk/res/android“
从List\u Item.xml
@SK9 stacktrace中的第二个LinearLayout
”added@MD是否只在根元素上设置了它??“现在就开始尝试。”约翰·亚,我知道。错误来自不同的地方,但这只是您的更正。我删除了它,现在不再包含标题。我在包含的布局中设置了id,但发生了相同的情况。
<include android:id="@+id/headerItem" // remove it
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="76dip"
android:background="@color/standard_background"
layout="@layout/List_Item"/>
layout = (RelativeLayout)view.findViewById(R.id.headerItem);
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>