English 中文(简体)
在安乐文中增加形象
原标题:Getting NullPointer when adding image in Android

I am trying to set my image in my view at acertain point in my animation. However I am getting a nullpointer exception. I know I am doing a rookie mistake but I cant figure out what I should do... When I set my image onStart()it works fine...But when I set the image in the onAnimationEnd I am getting the null pointer...Please Help!

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;
}


}

ERROR:

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)
最佳回答

http://www.ohchr.org。 在AnimationEnd(注:verification this using the debugger!)上,你就宣布无效。 必须认识到: 无论在什么时候,都可能杀掉你的活动。 这项活动在收到AnimationEnd时重新编号(即:onCreate),但并未显示(onStart)。 这意味着该项活动的<代码>reizeImage2尚未形成,并且仍然无效。

容易的解决办法是,在<代码>onCreate上将<代码>reizeImage2改为

另一种解决办法是,在上储存,并在“条码>上填报<>。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...