Reputation: 321
I am trying to display a image(edited) say for every 1sec, i came across AsyncTask and thought that it might work. Below is the code for that,
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);
}
}
}
And when I call this activity I am getting the below error message in 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
Upvotes: 0
Views: 1656
Reputation: 697
I think you should transfer this code:
ImageView setimage = (ImageView) findViewById(R.id.setimage);
After the setContentView() method..
This make the imageview null because there is no resource android will find the R.id.setimage
see this link:
Android setContentView()
onCreate() method explains further
http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29
Upvotes: 1
Reputation: 1603
public void updateImage()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
updateImage();
}
},1000);
//Update image in imageview here....
}
Upvotes: 0