0

So I have an imageview1 that when it is clicked on a new activity is started where the same image is displayed in a new imageview2 on the next activity. The imageview1 that gets clicked on is accessed by a listview on my mainactivity. The listview is populated by an array and each listview item has a coresponding image.

So when a listview item is clicked it starts a new activity where imageview1 resides. This imageview is populated using an xml typedarray which references the drawable image.

Im having trouble passing the image from my first imageview to the next one on the new activity. I'm getting a null pointer exception that I've included below. Im very new to programming and am not really sure how to fix the issue. Any help would be really appreciated.

heres my routeDetails class which contains the first imageview

public class RouteDetails extends AppCompatActivity {

ImageView routeImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_route_details);

    //TextView for route details
    final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
    routeDetailsView.setText(getIntent().getExtras().getString("route"));

    //ImageView for route details
    routeImage = (ImageView) findViewById(R.id.routeImage);
    routeImage.setImageResource(getIntent().getIntExtra("imageResourceId", 0));


    //passing image to fullscreenimage
    routeImage.setDrawingCacheEnabled(true);
    routeImage.buildDrawingCache();
    final Bitmap bitmap = routeImage.getDrawingCache();

////////// Trying to pass image to new activity

   routeImage.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view)
        {
            Intent i = new Intent(RouteDetails.this, FullScreenImage.class);
            //Bitmap _bitmap; // your bitmap
            ByteArrayOutputStream _bs = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
            i.putExtra("byteArray", _bs.toByteArray());
            startActivity(i);
        }

    });


////////////
}

and heres my fullscreenimage class im trying to pass the image to

public class FullScreenImage extends AppCompatActivity {

TouchImageView routeImageFull;

@Override
protected void onCreate(Bundle saveInstanceState){
    super.onCreate(saveInstanceState);


    setContentView(R.layout.full_screen_image);
    routeImageFull = (TouchImageView) findViewById(R.id.fullScreenImageView);


    if(getIntent().hasExtra("byteArray")) {
        TouchImageView routeImageFull= new TouchImageView(this);
        Bitmap bitmap = BitmapFactory.decodeByteArray(
                getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
        routeImageFull.setImageBitmap(bitmap);
    }
}
}

and heres the error im getting

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.zach.listview, PID: 3429
              java.lang.NullPointerException: Attempt to invoke virtual        method 'boolean     android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int,     java.io.OutputStream)' on a null object reference
                  at     com.example.zach.listview.RouteDetails$1.onClick(RouteDetails.java:47)
                  at android.view.View.performClick(View.java:5610)
                  at android.view.View$PerformClick.run(View.java:22265)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at     android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
zsh5032
  • 141
  • 1
  • 10
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Code-Apprentice Nov 18 '16 at 20:52
  • Please read the above link for suggestions about how to figure out what casues the error. You should start on line 47 of RouteDetails.java. – Code-Apprentice Nov 18 '16 at 20:53
  • `routeImage.getDrawingCache()` has returned you null, apparently – OneCricketeer Nov 18 '16 at 20:53
  • It appears that line 47 of RouteDetails.java is `bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);`. This means that `bitmap` is set to `null`. You should use your debugger to find out why. – Code-Apprentice Nov 18 '16 at 20:55
  • Unsolicited advice: The method you are using to pass an image here is very memory intensive. An alternative is to pass a file name or URL and let the second activity load it from there. – Code-Apprentice Nov 18 '16 at 20:56
  • how would I aquire and then pass the imageview image file location to the next activity? this is just one of many things i've tried with no success. – zsh5032 Nov 18 '16 at 21:01

1 Answers1

0

Rather than passing the whole bitmap. You can simply send the resource id you are getting here to another activity and can show image by same way, you are doing here. Below is the changed code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_route_details);

    //TextView for route details
    final TextView routeDetailsView = (TextView)    
    findViewById(R.id.routeDetailsView);
    routeDetailsView.setText(getIntent().getExtras().getString("route"));

    //ImageView for route details
    routeImage = (ImageView) findViewById(R.id.routeImage);

    int mImageResource = getIntent().getIntExtra("imageResourceId", 0);
    routeImage.setImageResource(mImageResource);


   //passing image to fullscreenimage
   routeImage.setDrawingCacheEnabled(true);
   routeImage.buildDrawingCache();
   final Bitmap bitmap = routeImage.getDrawingCache();

   // Trying to pass image to new activity

   routeImage.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view)
        {
            Intent i = new Intent(RouteDetails.this,FullScreenImage.class);
            i.putExtra("mImageResource", mImageResource);
            startActivity(i);
        }

   });
}
UMESH0492
  • 1,701
  • 18
  • 31
  • 1
    You are my hero! thats what I was originally trying to do but gave up on it. It works perfect thank you so much. – zsh5032 Nov 18 '16 at 21:28