2014-01-17 44 views
-1

我试图让我的AlertDialog框发送消息。为此,我在对话框的按钮上使用了SmsManagers。我创建的对话框使用自定义布局。我想要的是,当我点击“确定”按钮时,一条消息将被发送到我在SmsManager中声明的预定义数字。相反,当我确定按钮单击该应用程序崩溃的AlertDialog箱AlertDialog框发送消息

代码:

public class MainActivity extends Activity implements OnClickListener { 
Button addnew, dialog; 
ListView lvInb; 
ArrayAdapter<String> 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<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<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1 , fetchInbox()); 
     lvInb.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     lvInb.setAdapter(adapter); 
    } 
} 

public ArrayList<String> fetchInbox() 
{ 
    ArrayList<String> sms = new ArrayList<String>(); 
    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(); 
    } 


} 

}像

而且logcat的是显示错误:

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) 
+0

邮政全logcat的,应该有一个“产生的原因:”一部分是相当重要的。 –

+0

您是否已将权限添加到manifest.xml中? –

+0

editSMS是否正确启动?也许它是空的,请复制您的整个Logcat跟踪 –

回答

0

请删除线alertDialog2.show();

并使用这些行

AlertDialog alertDialog = alertDialog2.create(); 

       // show it 
       alertDialog.show(); 

也做不来的清单

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission> 
+0

应用程序仍然崩溃,如果我点击确定按钮 –

0

尝试添加的权限,用来初始化EditText象下面这样的MainActivityOnCreate

editSMS = (EditText) findViewById(R.id.idname); 

editSMS

+0

Y我已经做到了,但仍然崩溃 –

+0

@ user3172071发布完整的'MainActivity.java' – Hariharan

+0

@ Hariharan Tamilan检查我的主要活动,我发布我的完整MainActivity.java –

0

字符串sms = editSMS.getText()。toString(); 。

改变这对

字符串SMS = input.getText()的toString();

0

消息长度可能是问题。尝试使用sendMultipartTextMessage()方法而不是sendTextMessage()。 下面是我的示例代码:

private static void sendMessage(String message){ 

    String number = m_Text; 

    try { 
     SmsManager smsManager = SmsManager.getDefault(); 
     //smsManager.sendTextMessage(number, null, message, null, null); 
     ArrayList<String> messageParts = smsManager.divideMessage(message); 

     smsManager.sendMultipartTextMessage(number, null, messageParts, null, null); 
     Toast.makeText(sContext, "Message Sent", 
       Toast.LENGTH_LONG).show(); 
    } catch (Exception ex) { 
     Toast.makeText(sContext,ex.getMessage(), 
       Toast.LENGTH_LONG).show(); 
     ex.printStackTrace(); 
    } 
}