2012-03-11 61 views
0

我是android開發中的新手。我擅長Java,但不擅長Android API。這是我的工作申請測試計劃,所以我需要幫助來解決問題。我創建了一個對話框,通過按下按鈕來調用,但我無法訪問對話框的textview(應用程序崩潰時,我試圖達到它 - NullPointerException)即使Eclipse IDE沒有發現任何問題。我幾乎包括所有的代碼,因爲我不知道該怎麼排除在對話框佈局Android:無法訪問對話框TextView

custom_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/layout_root" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="10dp" 
      > 
    <TextView android:id="@+id/dlgText" 
       android:layout_width="fill_parent" 
       android:layout_height="50dp" 
       android:textColor="#FFF" 
       /> 
</LinearLayout> 

BalticAmadeusActivity.java

public class BalticAmadeusActivity extends Activity{ 
    /** Called when the activity is first created. */ 
    Button btnApie; 
    Button btnForma; 
    Dialog dlgApie; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     btnApie = (Button) findViewById(R.id.btnApie); 
     btnForma = (Button) findViewById(R.id.btnForma); 
     btnApie.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 
       if (v.equals(btnForma)) { 
        btnForma(); 
       } else if (v.equals(btnApie)) { 
        btnApie(); 
       } 
       return false; 
      } 
     }); 
     dlgApie = new Dialog(this.getApplicationContext(), android.R.style.Theme_Translucent_NoTitleBar_Fullscreen); 
     dlgApie.setContentView(R.layout.custom_dialog); 
     TextView dlgText = (TextView) findViewById(R.id.dlgText); 
     dlgText.setText("Gera uzduotis"); //at this line, app crashes 

     //dlgText.setText("U\u017Eduotis s\u0117kmingai \u012Fgyvendinta"); 
    } 

    private void btnApie() { 
     // TODO Auto-generated method stub 
     dlgApie.show(); 
    } 

    private void btnForma() { 
     // TODO Auto-generated method stub 

    } 
} 
+0

那麼你的main.xml中是什麼樣子? – Phil 2012-03-11 18:34:23

回答

1

搜索(現在您正在搜索在活動佈局):

TextView dlgText = (TextView) dlgApie.findViewById(R.id.dlgText); 
+0

感謝您的支持! – Jakob 2012-11-29 20:17:49