Android布局xml导致npe
我有一个空指针异常问题,我不明白为什么。请不要将此标记为线程的dup“空指针异常和如何修复”,因为该线程根本不相关 我知道什么是npe。因此,我不需要解释 因此,对于这个问题: 我有一个自定义布局的对话框 布局xml文件包含一个内部带有tablelayout的linearlayout。当我完全移除tablelayout时,npe就会消失。因此,我得出结论,XML肯定有问题 然而,Android studio没有给出任何错误 下面是指向相关xml的链接: 编辑: 以下是完整的XML代码:Android布局xml导致npe,android,xml,layout,nullpointerexception,dialog,Android,Xml,Layout,Nullpointerexception,Dialog,我有一个空指针异常问题,我不明白为什么。请不要将此标记为线程的dup“空指针异常和如何修复”,因为该线程根本不相关 我知道什么是npe。因此,我不需要解释 因此,对于这个问题: 我有一个自定义布局的对话框 布局xml文件包含一个内部带有tablelayout的linearlayout。当我完全移除tablelayout时,npe就会消失。因此,我得出结论,XML肯定有问题 然而,Android studio没有给出任何错误 下面是指向相关xml的链接: 编辑: 以下是完整的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();
}
@覆盖
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
Dialog=newdialog(getContext());
setContentView(R.layout.alarm\u details\u对话框);
dialog.show();
}
要设置对话框的自定义布局,您不需要像在以下代码中那样获取活动的引用:
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.
尝试上述更改,如果您的日志出现错误,请发布日志。问题肯定与您的
元素有关。即使在布局编辑器中,它也显示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
我认为,上述重复的
儿童要求身高与父母相配的层次结构才是造成错误的真正原因。我不知道确切的问题,但渲染器报告了与高度相关的NPE
如果您的
具有基于wrap\u内容的层次结构,则可以解决此问题。下面是更新后的
代码。我还将
元素的宽度更新为0dp
,因为它们使用了layout\u 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>
希望这有帮助~如果你觉得这是问题的原因,也可以共享你的xml。通过手机发帖,但我可以尝试从我的其他帖子复制。SecI将稍后在此线程中发布它。我愚蠢的Iphone不允许我从链接的线程复制文本。当我在计算机上时会更新。当你这样做时请告诉我。我有一些建议。我现在已经添加了XML,并且已经尝试过了。相同的NPE,但是当我从XML文件中删除tablelayout,从而只保留linearlayout时,它工作得很好。logcat或npe中没有错误。@ThomasHolden让我试试。只要运行它,就有了npe!我会找到答案的,等等!它必须是xml中的某种东西。我只是不明白。。。谢谢你尝试。顺便说一句,我会尝试一下。奇怪的是,我在android studio中没有看到任何错误msgs。