2012-04-08 69 views
0

我想在我的視圖中設置我的圖像在我的動畫中的某一點。但是我得到一個空指針異常。我知道我正在做一個菜鳥的錯誤,但我不知道我該怎麼辦... 當我設置我的形象onStart()它工作正常...但是當我在onAnimationEnd設置圖像我得到空指針...請幫忙!在Android中添加圖像時獲取NullPointer

public class PhotoSyncActivity extends Activity implements AnimationListener { 

private Bitmap[] images; 
private AnimationPhotoViewA apa1; 
private AnimationPhotoViewB apa2; 
private static final int move1 = 1; 
private static final int move2 = 2; 
private static final int move3 = 3; 
private static final int move4 = 4; 
private static final int move5 = 5; 
private int animationmove = 0; 
ArrayList<String> photoPaths; 
Bitmap resizedimage2= null; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.photo_sync_screen); 
    ArrayList<String> photoPaths = new ArrayList<String>(); 
    photoPaths = getAllPhotos(Environment.getExternalStorageDirectory(), 
      photoPaths); 
    images = new Bitmap[photoPaths.size()]; 
    Log.v(ACCESSIBILITY_SERVICE, "photo array!" + photoPaths); 

    apa1 = (AnimationPhotoViewA) findViewById(R.id.animation_viewA); 
    apa2 = (AnimationPhotoViewB) findViewById(R.id.animation_viewB); 
    animationmove = PhotoAnimationProcess.moveOne(this,apa1,animationmove); 

    File imgFile1 = new File(photoPaths.get(0)); 

    File imgFile2 = new File(photoPaths.get(1)); 

    if (imgFile1.exists() && imgFile2.exists()) 
    { 
     images[0] = decodeFile(imgFile1); 
     images[1] = decodeFile(imgFile2); 
    } 
} 

@Override 
protected void onStart() { 
    // TODO Auto-generated method stub 
    super.onStart(); 

    // RESIZE THE IMAGE TO A STANDARD SIZE 
    Bitmap resizedimage1 = setPictureSize(images[0]); 
    resizedimage2 = setPictureSize(images[1]); 

    // SET IMAGE IN THE VIEW 

    apa1.setImageBitmap(resizedimage1); 
    apa2.setImageBitmap(resizedimage2);//HERE THE CODE WORKS WITH NO PROBLEM!! 

private ArrayList<String> getAllPhotos(File dir,ArrayList<String> photoPaths) 
     { 
    File[] files = dir.listFiles(); 
    if (files != null) 
    { 
     for (File f : files) 
     { 
      if (f != null) 
      { 
       if (f.isDirectory()) 
       { 
        getAllPhotos(f, photoPaths); 
       } else 
       { 
        String filePath = f.getAbsolutePath(); 
        if (filePath.matches(".*\\.(jpeg|jpg)$")) 
        { 
         photoPaths.add(filePath); 
        } 
       } 
      } 
     } 
    } 
    return photoPaths; 
} 

public void onAnimationStart(Animation animation) 
{ 
    // TODO Auto-generated method stub 

} 

public void onAnimationEnd(Animation animation) 
{ 
    // TODO Auto-generated method stub 

    switch (animationmove) 
    { 
    case move1: 
     animationmove = PhotoAnimationProcess.moveOne(this, apa1, animationmove); 
     break; 
    case move2: 

     //HERE I AM GETTING THE NULL POINTER WHEN SETTING IMAGEapa2.setImageBitmap(resizedimage2); 
     animationmove = PhotoAnimationProcess.moveTwo(this,apa1,animationmove); 

     break; 
    case move3: 
     animationmove = PhotoAnimationProcess.moveThree(this,apa1,animationmove); 
     break; 
    case move4: 
     animationmove = PhotoAnimationProcess.moveFour(this,apa1,animationmove);; 
     break; 
    case move5: 
     animationmove = PhotoAnimationProcess.moveFive(this,apa1,animationmove); 
     break; 

    default: 
     break; 
    } 

    Log.v(ALARM_SERVICE, "Animation Type" + animation.toString()); 

} 



public void onAnimationRepeat(Animation animation) { 
    // TODO Auto-generated method stub 

} 


// Downgrade the photos 
private Bitmap decodeFile(File f) 
{ 
    Bitmap ret = null; 
    try 
    { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f), null, o); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE = 70; 

     // Find the correct scale value. It should be the power of 2. 
     int scale = 1; 
     while (o.outWidth/scale/2 >= REQUIRED_SIZE 
       && o.outHeight/scale/2 >= REQUIRED_SIZE) 
      scale *= 2; 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     ret = BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } 
    catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 

    } 
    return ret; 

} 

private Bitmap setPictureSize(Bitmap bitmapOrg) 
{ 

    int width = bitmapOrg.getWidth(); 

    int height = bitmapOrg.getHeight(); 

    int newWidth = 250; 

    int newHeight = 250; 

    // calculate the scale - in this case = 0.4f 

    float scaleWidth = ((float) newWidth)/width; 

    float scaleHeight = ((float) newHeight)/height; 

    Matrix matrix = new Matrix(); 

    matrix.postScale(scaleWidth, scaleHeight); 
    // matrix.postRotate(x); 
    // this will create image with new size 
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, 
      height, matrix, true); 
    return resizedBitmap; 
} 


} 

錯誤:

04-08 21:27:14.764: E/AndroidRuntime(1654): FATAL EXCEPTION: main 
04-08 21:27:14.764: E/AndroidRuntime(1654): java.lang.NullPointerException 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.graphics.Canvas.throwIfRecycled(Canvas.java:972) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.graphics.Canvas.drawBitmap(Canvas.java:998) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at com.uiu.registrationwizard.activities.AnimationPhotoViewB.drawPicture(AnimationPhotoViewB.java:79) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at com.uiu.registrationwizard.activities.AnimationPhotoViewB.onDraw(AnimationPhotoViewB.java:67) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.View.draw(View.java:7014) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.ViewGroup.drawChild(ViewGroup.java:1732) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.View.draw(View.java:7017) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.ViewGroup.drawChild(ViewGroup.java:1732) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.View.draw(View.java:7017) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.widget.FrameLayout.draw(FrameLayout.java:357) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.ViewGroup.drawChild(ViewGroup.java:1732) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.View.draw(View.java:7017) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.widget.FrameLayout.draw(FrameLayout.java:357) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2054) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.ViewRoot.draw(ViewRoot.java:1632) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1335) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1991) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.os.Looper.loop(Looper.java:150) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at android.app.ActivityThread.main(ActivityThread.java:4385) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
04-08 21:27:14.764: E/AndroidRuntime(1654):  at dalvik.system.NativeStart.main(Native Method) 
+1

發佈堆棧跟蹤。 – m0skit0 2012-04-08 18:23:56

+0

您也應該發佈setPictureSize()方法 – andreasg 2012-04-08 18:25:20

+0

感謝您的關注!我加了兩個 – user182192 2012-04-08 18:33:25

回答

0

從外觀上來看,resizedImage2爲空當您在onAnimationEnd設置(注:驗證這個使用調試器)。意識到Android可能會在任何時候終止您的活動,這一點很重要。在接收到onAnimationEnd後,該活動被重新創建(即調用onCreate),但沒有顯示(所以沒有onStart)。這意味着該活動的resizedImage2未初始化,並且仍爲空。

簡單的解決方法是在onCreate中初始化resizedImage2

另一種解決方案是將resizedImage2存儲在onSaveInstanceState中,如果存在該包,則在onCreate中將其恢復。