2016-08-11 104 views
-1

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

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

因此,这个问题:

我有一个自定义布局

布局XML文件包含一个内部有一个tablelayout一个的LinearLayout对话框。当我完全移除桌面布局时,猿会消失。因此,我得出结论,它必须在XML中出现问题。

但是,Android studio没有提供任何错误。

这里是有问题的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(); 

    } 
+1

也许共享您的XML太多,如果你觉得导致问题。 – Shaishav

+0

从手机发帖,但我可以尝试从我的其他线程复制。秒 –

+0

我会在稍后的帖子中发帖。我的愚蠢Iphone不会让我复制链接线程中的文本。当我在控制台时会更新。 –

回答

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> 

希望这有助于〜

+0

我会试试看。奇怪的是,我没有看到任何错误消息在android工作室。 –

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。

+0

已经尝试过。相同的NPE,但是当我从XML文件中删除tablelayout时,只留下linearlayout,它工作正常。在logcat或npe中没有错误。 –

+0

@ThomasHolden让我试试吧。 – Shaishav

+0

整个代码库或布局?它有用吗? –