私のコードの概要は次のとおりです。
public class MainActivity extends Activity {
Client client;
MyImageView iv;
Bitmap b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyImageView iv = new MyImageView(this);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.untitled);
iv=(MyImageView)findViewById(R.id.ImageView1);}
iv.setImageBitmap(bMap);
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.example.zooming.MyImageView
android:id="@+id/ImageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:clickable="true"
android:scaleType="matrix" />
</LinearLayout>
MyImgaeView.java
MyImageView extends View {
public MyImageView(Context ct){
super(ct);
//some other works
gestureDetector = new GestureDetector(new MyGestureDetector());
new ConnectTask().execute("");
}
//Other code
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent event) {
iv=(MyImageView)findViewById(R.id.ImageView1);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
iv.setImageBitmap(bMap);
return true;
}
}
class connectTask extends AsyncTask<String,Bitmap,Bitmap>
{
@Override
protected Bitmap doInBackground(String... message) {
//some code to get the bitmap
publishProgress(mybitmap);
return null;
}
@Override
public void onProgressUpdate(Bitmap... values) {
super.onProgressUpdate(values);
iv=(MyImageView)findViewById(R.id.ImageView1);
if(iv==null)
Log.i("Exception",ex.toString());
iv.setImageBitmap(values[0]);
}
}
}
iv は常に ConnectTask(AsynTask) で null を返しますが、MyGestureDetector では正常に動作しています...なぜですか? また、ConnectTask を MainActivity の内部クラスにすると、正常に動作します...!!