Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving issue with Intent Activities and Alert Dialogs
    text
    copied!<p>I was wondering if someone could help me. It may be good to have a fresh pair of eyes to tell me what I am doing wrong because I just can't find it. It's probably something trivial, but I cannot see it. I basically have a button that when clicked brings up a custom dialog and asks the user to make a choice between an SD Card or Device memory. I feel I have everything right for the most part it's just giving me an error similar to: onClick error on line71..</p> <p>Can anyone else see a problem..</p> <pre><code> public class MediaActivity extends Activity { /** Called when the activity is first created. */ AlertDialog.Builder builder; AlertDialog alertDialog; ImageButton sdImage; ImageButton deviceImage; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button startApp = (Button) findViewById(R.id.startButton); startApp.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Context mContext = MediaActivity.this; LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custlayout, (ViewGroup) findViewById(R.id.layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); //text.setText("Upload an Image or Video"); builder = new AlertDialog.Builder(mContext); builder.setView(layout); alertDialog = builder.create(); sdImage= (ImageButton) findViewById(R.id.sdImageButton); deviceImage = (ImageButton) findViewById(R.id.deviceImageButton); sdImage.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // Write your code here to invoke YES event Intent goToSD = new Intent(arg0.getContext(), Sdcard.class); startActivity(goToSD); } }); deviceImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }); // Showing Alert Message alertDialog.show(); } }); } } </code></pre> <p>Thanks a bunch I really appreciate this since I've been having trouble with this for awhile. It's probably some kind of a dumb mistake that I'm overlooking.</p> <p>Thank you again!</p> <p>UPDATE LOGCAT:</p> <p>06-26 17:54:35.119: D/AndroidRuntime(24486): Shutting down VM 06-26 17:54:35.119: W/dalvikvm(24486): threadid=1: thread exiting with uncaught exception (group=0x40015560) 06-26 17:54:35.119: E/AndroidRuntime(24486): FATAL EXCEPTION: main 06-26 17:54:35.119: E/AndroidRuntime(24486): java.lang.NullPointerException 06-26 17:54:35.119: E/AndroidRuntime(24486): at my.edu.org.MediaActivity$1.onClick(MediaActivity.java:71) 06-26 17:54:35.119: E/AndroidRuntime(24486): at android.view.View.performClick(View.java:2485) 06-26 17:54:35.119: E/AndroidRuntime(24486): at android.view.View$PerformClick.run(View.java:9080) 06-26 17:54:35.119: E/AndroidRuntime(24486): at android.os.Handler.handleCallback(Handler.java:587) 06-26 17:54:35.119: E/AndroidRuntime(24486): at android.os.Handler.dispatchMessage(Handler.java:92) 06-26 17:54:35.119: E/AndroidRuntime(24486): at android.os.Looper.loop(Looper.java:130) 06-26 17:54:35.119: E/AndroidRuntime(24486): at android.app.ActivityThread.main(ActivityThread.java:3683) 06-26 17:54:35.119: E/AndroidRuntime(24486): at java.lang.reflect.Method.invokeNative(Native Method) 06-26 17:54:35.119: E/AndroidRuntime(24486): at java.lang.reflect.Method.invoke(Method.java:507) 06-26 17:54:35.119: E/AndroidRuntime(24486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850) 06-26 17:54:35.119: E/AndroidRuntime(24486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) 06-26 17:54:35.119: E/AndroidRuntime(24486): at dalvik.system.NativeStart.main(Native Method) ////////////////////// implementation of onClick from droider's suggestions.</p> <pre><code>@Override public void onClick(View v) { switch(v.getId()) { case R.id.sdImageButton: Intent goToSD = new Intent(v.getContext(), Sdcard.class); startActivity(goToSD); Toast.makeText(getApplicationContext(), "sdclicked", Toast.LENGTH_SHORT).show(); break; case R.id.deviceImageButton: break; default: break; } } </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