Note that there are some explanatory texts on larger screens.

plurals
  1. POApplication force-closed when setting two onClickListeners
    text
    copied!<p>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.</p> <p>public class MainActivity extends Activity {</p> <pre><code>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 &gt; 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); } } }); } </code></pre> <p>};</p> <p>Edit:</p> <p>Errors from the logcat after pressing the button:</p> <pre><code>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~ </code></pre> <p>Edit:</p> <pre><code>@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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload