2010-11-17 39 views
0

如何檢測圖像視圖中是否有按鈕單擊時的圖片。我有一個顯示圖片和一些結果的類,但我不希望用戶能夠訪問這些結果(按下按鈕),直到結果寫入該類。在您班級的xml文件中調用一個id

所以,我想我需要檢查是否有圖片顯示在ImageView中。

因此,像:

 case R.id.previous_box: 
      if (R.id.photoResultView == null){ 
          //throw up a dialog box saying they need to snap a pic first. 

      } 


      Intent j = new Intent(DTF.this, Result.class); 
      startActivity(j); 
      return; 

但obviouslt R.id.photoResultView == null是不這樣做的正確方法?有誰知道怎麼樣?

坦克!

編輯:行184

if (photoResultView.getDrawable() == null) { 

編輯:

 case R.id.previous_box: 

      if (photoResultView.getDrawable() == null) { 
       showToast(DTF.this, "You have to take a picture first"); 
       return; 
      } 
      Intent j = new Intent(main.this, Result.class); 
      startActivity(j); 
      return; 

回答

1

嘗試,而不是if塊你目前有,這樣的:

ImageView photoResultView = (ImageView)findViewById(R.id.photoResultVew); 
if (photoResultView.getDrawable() == null) { 
    //throw dialog 
} 

ImageView.getDrawable()收益要麼被拉伸或null(如果沒有可繪製集合)。

基本上,說你有這樣的事情:

public class HelloAndroid extends Activity { 
    ImageView photoResultView; //this creates a class variable 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.main); 
     //this assigns it to the photoResultView you defined in your XML 
     photoResultView = (ImageView)findViewById(R.id.photoResultView); 
    } 
} 
+0

我把在,但它與非受迫性錯誤關閉 - 任何想法? – Sapp 2010-11-17 22:15:19

+0

你能編輯你的LogCat輸出到你的文章嗎? – kcoppock 2010-11-17 22:17:55

+0

photoResultView爲null,它沒有在視圖層次結構中聲明 – apps 2010-11-17 22:25:16

0
public class MyActivity extends Activity { 

private ImageView imageView; 

protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_records); 
    imageView = (ImageView) findViewById(R.id.image_view); 

} 

} 
相關問題