我试图让我的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)
邮政全logcat的,应该有一个“产生的原因:”一部分是相当重要的。 –
您是否已将权限添加到manifest.xml中? –
editSMS是否正确启动?也许它是空的,请复制您的整个Logcat跟踪 –