2016-08-11 97 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

整個代碼庫或佈局?它有用嗎? –