我的应用程序有页面回复(发送短信)....但我无法发送消息.....任何人帮我代码...我认为我的代码中有错误。 ...我怎么能删除那个错误...
Reply.java
public class Reply extends Activity {
EditText replyText;
Button reply;
String replyString,name,phoneNo, message,nickname,body;
SQLiteDatabase messagedb,mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.reply);
replyText=(EditText)findViewById(R.id.editTextReply);
reply=(Button)findViewById(R.id.buttonReply);
Bundle c = getIntent().getExtras();
name= c.getString("nick");
message=c.getString("body");
System.out.println("nickname in reply : "+name);
messagedb=this.openOrCreateDatabase("message",0, null);
messagedb.execSQL(
"CREATE TABLE IF NOT EXISTS tab2( nickname varchar,sender INT(13),body varchar)");
Cursor cur1=messagedb.rawQuery("select * from tab2 where nickname='"+name+"'", null);
while(cur1.moveToNext())
{
phoneNo=cur1.getString(cur1.getColumnIndex("sender"));
System.out.println("Number in cur1 : "+phoneNo);
}
TextView tv = (TextView)findViewById(R.id.textView2);
tv.setText(nickname);
reply.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
message=replyText.getText().toString();
sendSMS(phoneNo, message);
}
});
}
private void sendSMS(String phoneNumber, String message)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, MessageViewPage.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
}
}
reply.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editTextReply"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="@drawable/reply_edit"
android:paddingLeft="21dp"
android:paddingRight="14dp"
android:paddingTop="37dp"
android:paddingBottom="37dp"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp">
</EditText>
<Button
android:id="@+id/buttonReply"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:layout_centerHorizontal="true"
android:background="@drawable/white_bn"
android:layout_marginTop="35dp"
android:text="Reply" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="To : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp"
android:layout_marginLeft="50dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
我也使用了权限
<uses-permission android:name="android.permission.SEND_SMS"/>
07-06 22:42:35.515: W/dalvikvm(834): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-06 22:42:35.554: E/AndroidRuntime(834): FATAL EXCEPTION: main
07-06 22:42:35.554: E/AndroidRuntime(834): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dpn.pack/com.dpn.pack.MessageViewPage}: java.lang.NullPointerException
07-06 22:42:35.554: E/AndroidRuntime(834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-06 22:42:35.554: E/AndroidRuntime(834): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-06 22:42:35.554: E/AndroidRuntime(834): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-06 22:42:35.554: E/AndroidRuntime(834): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-06 22:42:35.554: E/AndroidRuntime(834): at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 22:42:35.554: E/AndroidRuntime(834): at android.os.Looper.loop(Looper.java:123)
07-06 22:42:35.554: E/AndroidRuntime(834): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-06 22:42:35.554: E/AndroidRuntime(834): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 22:42:35.554: E/AndroidRuntime(834): at java.lang.reflect.Method.invoke(Method.java:521)
07-06 22:42:35.554: E/AndroidRuntime(834): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-06 22:42:35.554: E/AndroidRuntime(834): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-06 22:42:35.554: E/AndroidRuntime(834): at dalvik.system.NativeStart.main(Native Method)
07-06 22:42:35.554: E/AndroidRuntime(834): Caused by: java.lang.NullPointerException
07-06 22:42:35.554: E/AndroidRuntime(834): at com.dpn.pack.MessageViewPage.onCreate(MessageViewPage.java:30)
07-06 22:42:35.554: E/AndroidRuntime(834): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-06 22:42:35.554: E/AndroidRuntime(834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-06 22:42:35.554: E/AndroidRuntime(834): ... 11 more
答案 0 :(得分:0)
修复MessageViewPage.java第30行中的NullPointerException
我认为在你到达sendSms()行之前你的问题已经存在了