2014-05-23 53 views
0

我目前正在開發一個需要自定義對話框片段的應用程序。該對話框由兩個元素組成,即消息和圖像或圓形進度欄視圖。對於圖像/進度條,我使用一個線性佈局作爲容器,我希望在運行時保留一個引用並動態添加圖像/進度條視圖。Android DialogFragment內部LinearLayout返回null

問題:我可以找到對textview的引用,但是當我嘗試獲取對內部線性佈局的引用時,它將返回null。我也嘗試膨脹父佈局,並保留對它的引用作爲rootview,然後嘗試rootview.findViewById()沒有成功。

奇數:我很容易找到內部TextView的引用,只有linearlayout返回null。

真的很感謝這個問題的一些指導,似乎是非常簡單的事情,但對於我的生活我無法弄清楚爲什麼它返回null。我已經發布了下面的代碼。

package com.app.devicetest.dialogs; 

import android.app.Dialog; 
import android.content.Context; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import com.app.devicetest.R; 

public class CustomDialog extends Dialog implements View.OnClickListener { 

    public final static int STYLE_SUCCESS = 1; 
    public final static int STYLE_FAILURE = 2; 
    public final static int STYLE_PROGRESS = 3; 

    private String message; 
    private TextView dialogMessage; 
    private LinearLayout dialogImageLayout; 

// private LinearLayout rootView; 

    private Context mContext; 

    public CustomDialog(Context context) { 
     super(context); 
     mContext = context; 
    } 

    public CustomDialog(Context context, int theme) { 
     super(context, theme); 
     mContext = context; 
    } 

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { 
     super(context, cancelable, cancelListener); 
     mContext = context; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

// NOTE: I have also tried using this method with rootView.findViewById but the inner linearlayout still returns null. 
//  LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
//  rootView = (LinearLayout) inflater.inflate(R.layout.dialog_custom, null); 

     setContentView(R.layout.dialog_custom); 

     dialogMessage = (TextView) findViewById(R.id.dialogMessage); 
     dialogMessage.setText(message); 

     dialogImageLayout = (LinearLayout) findViewById(R.id.dialogImageLayout); 

     setCancelable(true); 
     setCanceledOnTouchOutside(true); 
    } 


    public void setDialogMessage(String msg) { 
     message = msg; 
    } 

    public void dialogStyle(int style) { 
     // Note: Always returns null here. 
     Log.i("dialog", "dialog reference : " + ((dialogImageLayout == null) ? "null" : "not null")); 


     if(style == STYLE_SUCCESS) { 
     } 
     else if(style == STYLE_FAILURE) { 
     } 
     else if(style == STYLE_PROGRESS) { 

     } 
    } 
} 

下面是佈局文件:dialog_custom.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/custom_dialog_layout" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/container_dropshadow"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:id="@+id/dialogImageLayout"> 

     </LinearLayout> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="22sp" 
     android:id="@+id/dialogMessage" /> 

</LinearLayout> 

代碼調用對話框:

completeDialog = new CustomDialog(mContext, R.style.Theme_CustomDialog); 
    completeDialog.dialogStyle(CustomDialog.STYLE_SUCCESS); 
    completeDialog.setDialogMessage("Operation successfully completed."); 
    completeDialog.show(); 
+0

嘗試擴展'DialogFragment'來代替並重寫'onCreateView()'。 – Karakuri

+0

尷尬,你有沒有嘗試清理你的項目,並重建它? – dwbrito

+0

我已經嘗試過擴展對話框,並使用onCreateView()我一直在想,當我得到參考時,佈局可能沒有膨脹。再一次,這樣做的意義不大,因爲我能夠獲得對TextView的引用。 也嘗試清理和重建幾次:) 我使用Nexus 7來測試此代碼。 – ahmad

回答

0

我認爲這是值得一提的我是如何克服這個問題的人誰也遇到這個錯誤。

經過一番大量的研究,我找到了發生這種情況的原因。看起來方法dialogStyle()在內部視圖有膨脹之前被調用(可能是由於膨脹視圖需要更長的時間,與TextView相比)。如果在dialogImageLayout上的onCreateView上執行日誌打印,則會注意到引用不是NULL。

因此,當用戶調用dialogStyle()並在對話類中添加視圖時,如果您知道視圖適當膨脹並且您有對其的引用,則最好將引用變量保存在類內部的當前樣式中。