我试图显示一个图像(已编辑)说每1秒,我遇到 AsyncTask ,并认为它可能有用。下面是代码,
public class UpdateImageView extends Activity {
IntBuffer Pixel_Buffer;
Bitmap bitmapPreview;
ImageView setimage = (ImageView) findViewById(R.id.setimage);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_image);
BackgroundAsyncTask task = new BackgroundAsyncTask();
task.execute();
}
public class BackgroundAsyncTask extends
AsyncTask<Void, IntBuffer, Void> {
int myProgress;
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"onPostExecute", Toast.LENGTH_LONG).show();
// buttonStartProgress.setClickable(true);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"onPreExecute", Toast.LENGTH_LONG).show();
myProgress = 0;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
Pixel_Buffer = IntBuffer.allocate(bitmapPreview.getWidth()*bitmapPreview.getHeight());
bitmapPreview.copyPixelsToBuffer(Pixel_Buffer);
int currentpixelpos = 0,pixel_counter = 0;
// int[] temp_image_array = new int[bitmapPreview.getWidth()*bitmapPreview.getHeight()] ;
pixel_counter = 0;
for(int i=0; i<bitmapPreview.getHeight();i++)
{
for(int j=0; j<(bitmapPreview.getWidth());j++)
{
currentpixelpos = (int)(i*bitmapPreview.getWidth())+ (int)j;
Pixel_Buffer.put(currentpixelpos, 0x55000000);
}
publishProgress(Pixel_Buffer);
}
return null;
}
@Override
protected void onProgressUpdate(IntBuffer... buffer) {
// TODO Auto-generated method stub
// progressBar.setProgress(values[0]);
bitmapPreview.copyPixelsFromBuffer(Pixel_Buffer);
setimage.setImageBitmap(bitmapPreview);
}
}
}
当我调用此活动时,我在logcat中收到以下错误消息。
03-20 18:20:31.833: E/AndroidRuntime(1081): FATAL EXCEPTION: main
03-20 18:20:31.833: E/AndroidRuntime(1081): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.print/com.android.print.UpdateImageView}: java.lang.NullPointerException
03-20 18:20:31.833: E/AndroidRuntime(1081): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
03-20 18:20:31.833: E/AndroidRuntime(1081): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-20 18:20:31.833: E/AndroidRuntime(1081): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-20 18:20:31.833: E/AndroidRuntime(1081): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-20 18:20:31.833: E/AndroidRuntime(1081): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 18:20:31.833: E/AndroidRuntime(1081): at android.os.Looper.loop(Looper.java:123)
03-20 18:20:31.833: E/AndroidRuntime(1081): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-20 18:20:31.833: E/AndroidRuntime(1081): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 18:20:31.833: E/AndroidRuntime(1081): at java.lang.reflect.Method.invoke(Method.java:521)
03-20 18:20:31.833: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-20 18:20:31.833: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-20 18:20:31.833: E/AndroidRuntime(1081): at dalvik.system.NativeStart.main(Native Method)
03-20 18:20:31.833: E/AndroidRuntime(1081): Caused by: java.lang.NullPointerException
03-20 18:20:31.833: E/AndroidRuntime(1081): at android.app.Activity.findViewById(Activity.java:1637)
03-20 18:20:31.833: E/AndroidRuntime(1081): at com.android.print.UpdateImageView.<init>(UpdateImageView.java:25)
03-20 18:20:31.833: E/AndroidRuntime(1081): at java.lang.Class.newInstanceImpl(Native Method)
03-20 18:20:31.833: E/AndroidRuntime(1081): at java.lang.Class.newInstance(Class.java:1429)
03-20 18:20:31.833: E/AndroidRuntime(1081): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-20 18:20:31.833: E/AndroidRuntime(1081): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
03-20 18:20:31.833: E/AndroidRuntime(1081): ... 11 more
答案 0 :(得分:1)
我认为您应该传输此代码:
ImageView setimage = (ImageView) findViewById(R.id.setimage);
在setContentView()方法之后..
这使得imageview为null因为没有资源android会找到R.id.setimage
请看这个链接:
Android setContentView()
onCreate()方法进一步解释
http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29
答案 1 :(得分:0)
public void updateImage()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
updateImage();
}
},1000);
//Update image in imageview here....
}