0

Here is my code :

    btnSendSMS.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            final String phoneNo = txtPhoneNo.getText().toString();

            final String message = '\u00AB' + "smsmks" + '\u00BB' + ' '
                    + txtMessage.getText().toString();
            if ((phoneNo.length() > 0) && (message.length() > 0)) {
                //pilihan, mengirim citra gambar atau tidak
                final CharSequence[] items = { "Text", "Image" };
                AlertDialog.Builder kk = new AlertDialog.Builder(getBaseContext());
                kk.setTitle("Pilih Metode Penginputan");
                kk.setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int x) {
                        Intent i = null;
                        if ("Text".equals(items[x])) {
                            sendSMS(phoneNo, message);
                            Toast.makeText(getBaseContext(), phoneNo,
                                    Toast.LENGTH_SHORT).show();

                        } else {
                            i = new Intent(TulisPesanLontara.this,
                                    KonversiGambar.class);
                            startActivity(i);
                        }
                    }
                }).show();  

            } else
                Toast.makeText(getBaseContext(),
                        "Please enter both phone number and message.",
                        Toast.LENGTH_SHORT).show();
        }
    }

No error found, but if i run, and i click Items, I Getting force close. Can u help me? Thanks guys (sorry bad english)

Here my logcat

11-06 15:48:27.658: D/fastDelete(5657): ============= end to process delete event ============================ 11-06 15:48:28.307: I/fastDelete(5657): !!!fastDelete phase ended!!! 11-06 15:48:30.371: D/AndroidRuntime(5657): Shutting down VM 11-06 15:48:30.371: W/dalvikvm(5657): threadid=1: thread exiting with uncaught exception (group=0x40020648) 11-06 15:48:30.373: E/AndroidRuntime(5657): FATAL EXCEPTION: main 11-06 15:48:30.373: E/AndroidRuntime(5657): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 11-06 15:48:30.373: E/AndroidRuntime(5657): at android.view.ViewRoot.setView(ViewRoot.java:568) 11-06 15:48:30.373: E/AndroidRuntime(5657): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:209) 11-06 15:48:30.373: E/AndroidRuntime(5657): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:123) 11-06 15:48:30.373: E/AndroidRuntime(5657): at android.app.Dialog.show(Dialog.java:272) 11-06 15:48:30.373: E/AndroidRuntime(5657): at android.app.AlertDialog$Builder.show(AlertDialog.java:849) 11-06 15:48:30.373: E/AndroidRuntime(5657): at com.ariefspekta.smsmakassar.TulisPesanLontara$4.onClick(TulisPesanLontara.java:177) 11-06 15:48:30.373: E/AndroidRuntime(5657): at android.view.View.performClick(View.java:2535) 11-06 15:48:30.373: E/AndroidRuntime(5657): at android.view.View$PerformClick.run(View.java:9130) 11-06 15:48:30.373: E/AndroidRuntime(5657): at android.os.Handler.handleCallback(Handler.java:618) 11-06 15:48:30.373: E/AndroidRuntime(5657): at android.os.Handler.dispatchMessage(Handler.java:123) 11-06 15:48:30.373: E/AndroidRuntime(5657): at android.os.Looper.loop(SourceFile:351) 11-06 15:48:30.373: E/AndroidRuntime(5657): at android.app.ActivityThread.main(ActivityThread.java:3850) 11-06 15:48:30.373: E/AndroidRuntime(5657): at java.lang.reflect.Method.invokeNative(Native Method) 11-06 15:48:30.373: E/AndroidRuntime(5657): at java.lang.reflect.Method.invoke(Method.java:538) 11-06 15:48:30.373: E/AndroidRuntime(5657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 11-06 15:48:30.373: E/AndroidRuntime(5657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:659) 11-06 15:48:30.373: E/AndroidRuntime(5657): at dalvik.system.NativeStart.main(Native Method) 11-06 15:48:37.789: I/Process(5657): Sending signal. PID: 5657 SIG: 9

Arief Rivai
  • 228
  • 6
  • 19

2 Answers2

0

just replace getBaseContext() with either getApplicationContext() or use ActivityName.this.

Sumant
  • 2,775
  • 2
  • 22
  • 30
0

just pass the reference of whole application use getApplicationContext() at the place of getBaseContext, because getBaseContext take only particular activity reference..

Shivansh Saxena
  • 204
  • 2
  • 8