0

そこで、このチュートリアルを使用して、画像を表示するカスタム トーストを作成しました。

ここでやりたいことは、画像リソースを渡して、トースト メソッドにその画像を表示するように指示できるようにすることです。

だから私はこれを持っています:

 private void callToast(int image_resource)
{               
    LayoutInflater inflater = getLayoutInflater();
    View v = inflater.inflate(R.layout.mycustomtoast, (ViewGroup) findViewById(R.id.toast_layout));

    /*set the image*/           
    ImageView iv = (ImageView)findViewById(R.id.toast_iv);
    iv.setImageResource(image_resource); 

    Toast t = new Toast(this);
    t.setView(v);
    t.setGravity(Gravity.CENTER,0,0); 

    t.show();  
}

findViewById が null を返すようになりました...下にある XML が使用されていないため、わかりましたか?

カスタム トースト イメージ ビューのソースを変更するにはどうすればよいですか?

4

1 に答える 1

2

ここで、参照を表示するのを忘れましたv

ImageView iv = (ImageView)v.findViewById(R.id.toast_iv);

解決

Toast t = new Toast(this);
t.setView(v);

ImageView iv = (ImageView)v.findViewById(R.id.toast_iv);
iv.setImageResource(image_resource); 

t.setGravity(Gravity.CENTER,0,0); 
于 2012-06-26T06:02:02.743 に答える