Android布局xml导致npe

时间:2016-08-11 13:29:43

标签: android xml layout nullpointerexception dialog

我有一个空指针异常的问题,我不明白为什么。请不要将此标记为线程“空指针异常以及如何修复它”的副本,因为该线程根本不相关。

我知道什么是npe。因此,我不需要解释。

所以,问题是:

我有一个带有自定义布局的对话框

布局xml文件包含一个内部有tablelayout的linearlayout。当我完全删除tablelayout时,npe消失了。因此,我得出结论,XML必须有问题。

然而,Android工作室没有出错。

以下是相关xml的链接:

https://stackoverflow.com/questions/38879730/null-pointer-exception-when-trying-to-open-a-dialogbox-from-fragment-android

编辑:

以下是完整的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 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"
android:weightSum="1">

<TextView
    android:layout_width="match_parent"
    android:layout_height="25dp"
    android:text="Alarm info"
    android:id="@+id/alarm_fragment_dialog_title"
    android:textSize="20dp"
    android:textColor="#000"
    android:textAlignment="center" />

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="376dp"
    android:layout_marginTop="15dp"
    android:layout_gravity="center_horizontal">

    <TableRow android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_column="1"
            android:text="Enhetens navn:"
            android:textColor="#000"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
            android:text="Ctrl-O"
            android:textColor="#000"
            android:layout_weight="5"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableRow>

    <TableRow android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_column="1"
            android:text="Tidspunkt:"
            android:textColor="#000"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
            android:text="Ctrl-O"
            android:textColor="#000"
            android:layout_weight="5"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableRow>

    <TableRow android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_column="1"
            android:text="Posisjon:"
            android:textColor="#000"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
            android:text="Ctrl-O"
            android:textColor="#000"
            android:layout_weight="5"
            android:layout_width="match_parent" />
    </TableRow>
</TableLayout>

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

</LinearLayout>

编辑2:添加Java代码

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        Dialog dialog = new Dialog(getContext());

        dialog.setContentView(R.layout.alarm_details_dialog);

        dialog.show();

   }

2 个答案:

答案 0 :(得分:0)

要将自定义布局设置为对话框,您无需像以下代码那样获取活动的引用:

dialog.setContentView(getActivity().findViewById(R.id.alarm_details_datalog));

您只需要引用您的布局文件:

dialog.setContentView(R.layout.layout_file_name); // not the id of your root container, but the name of your xml file itself.

尝试使用上述更改,如果您的错误发布在logcat上。

答案 1 :(得分:0)

问题肯定在于您的<TableLayout>元素。即使在布局编辑器中,它也显示NPE而无需运行。我注意到这可能是因为以下设置:

-TableLayout -> height = 376dp
    -TableRow -> height = match_parent
         -textview -> height = match_parent
         -textview -> height = match_parent
    -TableRow -> height = match_parent
         -textview -> height = match_parent
         -textview -> height = match_parent
    -TableRow -> height = match_parent
         -textview -> height = match_parent
         -textview -> height = match_parent

请求身高为<TableLayout>的重复match_parent个孩子的上述等级是我认为是错误的真正原因。我不知道确切的问题,但渲染器报告了高度相关的NPE。

如果您的<TableLayout>具有基于wrap_content的层次结构,则可以解决此问题。因此,下面是更新的<TableLayout>代码。我还将<TableRow>元素的宽度更新为0dp,因为它们使用的是layout_weight属性。

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="376dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_weight="3"
                android:text="Enhetens navn:"
                android:textColor="#000"/>

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="Ctrl-O"
                android:textColor="#000"/>
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_weight="3"
                android:text="Tidspunkt:"
                android:textColor="#000"/>

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="Ctrl-O"
                android:textColor="#000"/>
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_weight="3"
                android:text="Posisjon:"
                android:textColor="#000"/>

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="Ctrl-O"
                android:textColor="#000"/>
        </TableRow>
    </TableLayout>

希望这会有所帮助〜