私はこれに対する答えを見つけようとしましたが、これまでには成功していません。答えは基本的なオブジェクト指向の考え方にあると思います。ImageViewとjava(Android)のnew演算子を使用する対比
基本的に、私は正常に動作します。このコードを持っている:ここでは
public class Game extends Activity implements OnTouchListener{
ImageView button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button= (ImageView) findViewById(R.id.DaImageView);
button.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent e) {
Log.d(TAG, "Test output.");
return false;
}
}
はXMLファイルです:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_height="wrap_content"
android:layout_width="wrap_content">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/spinning_wheel2"
android:id="@+id/DaImageView"
android:longClickable="true" />
</LinearLayout>
しかし、私がやりたいことのようなものです:
button = new ImageView(this);
button.setId(R.id.DaImageView);
button.setOnTouchListener(this);
ここで、2行目は正しい表示を参照する必要があります。このようにすると、onTouchListenerは応答しません。これの理由は何ですか?私はImageViewのを拡張するカスタムクラスを持っているので、私はこのようにそれをやりたい
理由は、したがって、それは次のようにキャストすることはできませんされています
button = (customClass) findViewById(R.id.DaImageView);
私はあなたが持つかもしれないコメントに感謝します!
ありがとう、私が探していたもの!悪魔は細部にいます。 –