2012-03-20 189 views
0

我想顯示圖像(編輯)說每1秒,我碰到AsyncTask,並認爲它可能工作。下面是這個代碼,如何更新imageview中的圖像?

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

首先告訴我們,你得到了什麼錯誤,請嘗試提供堆棧跟蹤 – 2012-03-20 13:18:04

回答

0
public void updateImage() 
{ 
    new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       updateImage(); 
      } 
     },1000); 
//Update image in imageview here.... 
} 
+0

我想這很長回來,這延遲整個用戶界面,並在一段時間後得到最終圖像.... – jxgn 2012-03-20 13:18:58

+0

在單獨的線程中更新映像並在處理程序之後調用該線程。 – 2012-03-20 13:23:24

1

我想你應該轉移這個代碼:

ImageView setimage = (ImageView) findViewById(R.id.setimage); 

中的setContentView()方法後..

這使ImageView的空,因爲沒有資源的Android會發現R.id.setimage

看到這個鏈接:

的Android的setContentView()

http://developer.android.com/reference/android/app/Activity.html#setContentView%28android.view.View%29

onCreate()方法進一步解釋

http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29