2011-10-17 8 views
3

私は近いこのAndroidのJavaのセット画像リソース

if(num1.getText().equals("0")) { 
      num1.setText("1"); 
      ImageView hpdown1 = (ImageView)findViewById(R.id.hair); 
      hpdown1.setImageResource(R.drawable.haie2); 
     } 

に力を得るあなたは言いませんが、私は、nullポインタ例外を推測している。..

+2

ログのどこかに例外があると思われます。それを見つけてもっと多くの情報を提供します... –

答えて

2

を助けてください。 setAmageResourceを呼び出す前にhpdownnullでないことを確認してください。

15

ResourcesクラスのgetDrawableメソッドを使用できます。たとえば:

ImageView image = (ImageView) findViewById(R.id.image); 
Resources res = getResources(); /** from an Activity */ 
image.setImageDrawable(res.getDrawable(R.drawable.myimage)); 
+0

古いバージョンをサポートするには、ResourcesCompatを次のように使用してください:http://stackoverflow.com/a/29041466/1732338 –

0

何かがあなたの問題は、あなたのリソースがあるということであると言われます:それはRであると考えられるスペルの間違いだように私には、あなたのコードのコンテキストで、見えR.drawable.haie2を.drawable.hair2

2

getResources().<strike>getDrawable</strike>(R.drawable.myimage)); 

廃止されました:

image.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.menu_southamerica)); 
関連する問題