我试图让我的AlertDialog框发送消息。为此,我在对话框的按钮上使用了SmsManagers。我创建的对话框使用自定义布局。我想要的是,当我点击“确定”按钮时,会将一条消息发送到我在SmsManager中声明的预定义数字。相反,当我单击“确定”按钮时,应用程序崩溃
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 :(得分:0)
请删除第alertDialog2.show();
行
并使用这些行
AlertDialog alertDialog = alertDialog2.create();
// show it
alertDialog.show();
也不要在清单
添加权限<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
答案 1 :(得分:0)
尝试在EditText
MainActivity
内OnCreate
editSMS = (EditText) findViewById(R.id.idname);
,如下所示
editSMS
为你{{1}}
答案 2 :(得分:0)
String sms = editSMS.getText()。toString();
将此更改为
String sms = input.getText()。toString();
答案 3 :(得分: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();
}
}