我的代码大纲是:
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 的内部类时,它的工作正常......!!