title: AlertDialog box to send message
body: <p>Im trying to make my AlertDialog box to send message. For that I've used SmsManagers on button of dialog box. The dialog box I've created uses custom layout. What I want is that when I click on "ok" button then a message will be sent to a predefined number which I declare in SmsManager. Instead, when I click on ok button the application crashes</p>
  
  <p>Code for AlertDialog box :</p>
  
  <pre><code>public class MainActivity extends Activity implements OnClickListener {
  Button addnew, dialog;
  ListView lvInb;
  ArrayAdapter&lt;String&gt; adapter;
  int count = 0;
  EditText editSMS;
  SharedPreferences prefs=null;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ListView lvInb = (ListView) findViewById(R.id.lvInb);
      addnew = (Button)findViewById(R.id.btnaddnew);
      addnew.setOnClickListener(this);
      dialog = (Button)findViewById(R.id.btnproperty);
      dialog.setOnClickListener(this);
  
      editSMS = (EditText) findViewById(R.id.editSMS);
      prefs = PreferenceManager.getDefaultSharedPreferences(this);
      count=prefs.getInt("count", 0);
      for(int i=0;i&lt;count;i++){
          Button myButton = new Button(this);
          myButton.setOnClickListener(this);
          myButton.setText("New Button");
          myButton.setId(count);
          LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
          LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
          ll.addView(myButton, lp);
      }
      lvInb = (ListView) findViewById(R.id.lvInb);
      lvInb.setOnCreateContextMenuListener(this);
      if(fetchInbox()!=null)
      {
          ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_activated_1 , fetchInbox());
          lvInb.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
          lvInb.setAdapter(adapter);
      }
  }
  
  public ArrayList&lt;String&gt; fetchInbox()
  {
       ArrayList&lt;String&gt; sms = new ArrayList&lt;String&gt;();
       Uri uriSms = Uri.parse("content://sms/inbox");
       Cursor cr = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null);
       cr.moveToFirst();
          while  (cr.moveToNext())
          {
               sms.add(cr.getString(1)+"\n"+cr.getString(3)+"\n");
          }
          return sms;
  
  
      }
  
  
  
  
  
  @Override
  public void onClick(View v) {
      if(v == dialog)
      {
  
  
          AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(MainActivity.this);
          alertDialog2.setTitle("Message");
          alertDialog2.setMessage("Enter Related Text");
          final EditText input = new EditText(MainActivity.this);
          LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
          input.setLayoutParams(lp);
          alertDialog2.setView(input);
          alertDialog2.setPositiveButton("Ok",new DialogInterface.OnClickListener(){
              public void onClick(DialogInterface dialog, int which){
  
                  String phoneNo = "111";
                   String sms = editSMS.getText().toString();
                   try {
                           SmsManager smsManager = SmsManager.getDefault();
                           smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                           Toast.makeText(getApplicationContext(), "SMS Sent!",Toast.LENGTH_LONG).show();
  
  
                   } catch (Exception e) {
                       Toast.makeText(getApplicationContext(),"SMS failed, please try again later!",Toast.LENGTH_LONG).show();
                       e.printStackTrace();
                   }
  
              }
  
  
          });
      alertDialog2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which){
              Toast.makeText(getApplicationContext(), "Reverting Changes", Toast.LENGTH_SHORT).show();
              dialog.cancel();
  
          }
      });
      alertDialog2.show();
  
      }
  
  
      if(v == addnew)
      {
  
          Button myButton = new Button(this);
          myButton.setText("New Button");
          LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
          LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
          ll.addView(myButton, lp);
          count++;
          Editor editor = prefs.edit();
          editor.putInt("count", count);
          editor.commit();
      }
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Also logcat is showing errors like:</p>
  
  <pre><code>Shutting down VM
  W/dalvikvm(2537): threadid=1: thread exiting with uncaught exception (group=0xb3a3bb90)
  FATAL EXCEPTION: main
  Process: com.example.helloworld, PID: 2470
  java.lang.NullPointerException
  com.example.helloworld.MainActivity$1.onClick(MainActivity.java:198)
  com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
  android.os.Handler.dispatchMessage(Handler.java:102)
  android.app.ActivityThread.main(ActivityThread.java:4998 at java.lang.reflect.Method.invokeNative(Native Method)
  java.lang.reflect.Method.invoke(Method.java:515)
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
  dalvik.system.NativeStart.main(Native Method)
  </code></pre>