Question

All of the code below works just as desired. That is, until I attempt to add a second onClickListener to another image. The code I am trying to add, and where I am trying to add it is listed as comments in the code below. Does anyone see anything that I'm missing here? I feel as though there shouldn't be any problems with this extra implementation.

public class MainActivity extends Activity {

ImageView mImage;
//ImageView journal;

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

    mImage = (ImageView)MainActivity.this.findViewById(R.id.Floaterimg);
    //journal = (ImageView)MainActivity.this.findViewById(R.id.journbtn);

    //journal.setOnClickListener(new View.OnClickListener() {

        //@Override
        //public void onClick (View v) {
            //Intent i = new Intent(MainActivity.this, 
                            //SecondActivity.class);
            //startActivity(i);
        //}
    //});

    final Handler randomizer = new Handler();
    final Runnable rrandomizer = new Runnable() {
        public void run() {

            RelativeLayout.LayoutParams params = new 
                               LayoutParams(LayoutParams.WRAP_CONTENT, 
                               LayoutParams.WRAP_CONTENT);
            params.topMargin = (int)(Math.random()*2000 + 1);
            params.leftMargin = (int)(Math.random()*3000 + 1); 

            mImage.setLayoutParams(params);

            randomizer.postDelayed(this, 5000);
        }
    };
    rrandomizer.run();

    mImage.setOnClickListener(new View.OnClickListener() {
        int numClicks = 0;

        @Override
        public void onClick(View arg0) {
            numClicks++;
            if(numClicks > 5) {

                Bitmap bitmap = 
                                     BitmapFactory.decodeResource(getResources(), 
                                     R.drawable.ic_launcher);     
                ByteArrayOutputStream baos = new 
                                                  ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
                byte[] b = baos.toByteArray();

                Intent intent = new Intent(MainActivity.this, 
                                                             SecondActivity.class);
                intent.putExtra("picture", b);

                }
            }

        });
}

};

Edit:

Errors from the logcat after pressing the button:

10-18 14:17:32.529: E/InputDispatcher(17677): Event_report_payload: Failed to write keypress event to kernel, error = 5
10-18 14:17:32.529: E/dhcpcd(7483): open_udp_socket: No such device
10-18 14:17:32.549: E/dhcpcd(7483): open_socket: No such file or directory
10-18 14:17:32.549: E/dhcpcd(7483): send_raw_packet: No such file or directory
10-18 14:17:32.609: E/InputDispatcher(17677): Event_report_payload: Failed to write keypress event to kernel, error = 5
10-18 14:17:32.759: E/AndroidRuntime(26780): Uncaught handler: thread main exiting due to uncaught exception
10-18 14:17:32.759: E/Monkey(26780): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
10-18 14:17:32.809: E/AndroidRuntime(26780): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
10-18 14:17:32.809: E/AndroidRuntime(26780): FATAL EXCEPTION: main
10-18 14:17:32.809: E/AndroidRuntime(26780): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.SecondActivity}: java.lang.NullPointerException
10-18 14:17:32.809: E/AndroidRuntime(26780):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:945)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at android.os.Looper.loop(Looper.java:130)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at android.app.ActivityThread.main(ActivityThread.java:3719)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at java.lang.reflect.Method.invokeNative(Native Method)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at java.lang.reflect.Method.invoke(Method.java:507)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at dalvik.system.NativeStart.main(Native Method)
10-18 14:17:32.809: E/AndroidRuntime(26780): Caused by: java.lang.NullPointerException
10-18 14:17:32.809: E/AndroidRuntime(26780):    at com.example.inkuesttest.SecondActivity.onCreate(SecondActivity.java:18)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-18 14:17:32.809: E/AndroidRuntime(26780):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
10-18 14:17:32.809: E/AndroidRuntime(26780):    ... 11 more
10-18 14:17:32.829: E/AndroidRuntime(26780): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
10-18 14:17:32.869: E/ActivityManager(17677): set systemprop back to original value~

Edit:

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

    Bundle extras = getIntent().getExtras();
//line18
    byte[] b = extras.getByteArray("picture");
//end line18

    Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
    ImageView image = (ImageView) findViewById(R.id.imageView1);

    image.setImageBitmap(bmp);
}
}
Was it helpful?

Solution

Your problem is that you are trying to access extras in your Intent but you aren't passing any. You do in the Intent below which is why that one works

intent.putExtra("picture", b);

but you don't do this in your other onClick(). You will either make sure that you are sending extras or to be safe do a null check in your SecondActivity with something like

  Bundle extras = getIntent().getExtras();
  //line18
  if (getIntent().getExtras() != null)
  {
      byte[] b = extras.getByteArray("picture");
  }

Then of course you will need to handle the lines below that if it is null.

Edit

If the two Buttons will do generally the same thing then you can See this answer to use the same method for both. Then you just switch on the id of the View to use different code depending on which was clicked. Something like

    @Override
    public void onClick(View v) {   // arg0 is an ugly name for a param so I changed it to v
       switch (v.getId())
       {
           case (R.id.Floaterimg):
              // do stuff if this image is clicked;
              break;
           case (R.id.journbtn):
              // do stuff if this image is clicked;
              break;
       }
}

then put common code such as your Intent outside of the switch statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top