2012-10-09 60 views
0

我有一個標籤活動caled「tabActivity」,在標籤活動內我想要一個按鈕「butDetail」在「saveImageActivity」顯示一個自定義對話框「detail.xml」。這是我的代碼,以顯示該對話框安卓定製對話框選項卡活動

public void butDetail(View v){ 
    final Dialog dialog = new Dialog(saveImageActivity.this); 
    dialog.setContentView(R.layout.detail); 
    dialog.setTitle("Detail Image"); 
    TextView filepath = (TextView)findViewById(R.id.txtfilepath); 
    TextView resolution = (TextView)findViewById(R.id.txtresolution); 
    filepath.setText("File Path : "); 
    resolution.setText("Resolution : "); 
    dialog.setCancelable(true); 
    dialog.show(); 
} 

爲什麼,如果我添加了「文件路徑」和「分辨率」它總是給人「顯示java.lang.NullPointerException」,如果我detele兩個variabel的對話框顯示,,, 這種情況下的任何解決方案??

回答

2

使用下面的代碼:

TextView filepath = (TextView)dialog.findViewById(R.id.txtfilepath); 
TextView resolution = (TextView)dialog.findViewById(R.id.txtresolution); 
+1

完全正確的。你應該「通過ID找到視圖」,但在適當的環境中! –

1

使用此代碼

private void showDiaalog() { 
      final Dialog dialog = new Dialog(Context); 
      dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE); 
      dialog.setContentView(R.layout.layoutfile); 

      dialog.setCancelable(true); 
      btnok = (Button) dialog.findViewById(R.id.btnOk); 

      btnok.setOnClickListener(new OnClickListener() { 

       public void onClick(View arg0) { 

         //some thing else 
        } 


       } 
      }); 
      Button btnCancel = (Button) dialog.findViewById(R.id.btncancel); 

      btnCancel.setOnClickListener(new OnClickListener() { 

       public void onClick(View arg0) { 

        dialog.dismiss(); 
       } 
      }); 

      dialog.show(); 
     }