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)