这是清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.SMSMessaging"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SMS"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是XML文件(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the phone number of recipient"
/>
<EditText
android:id="@+id/txtPhoneNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Message"
/>
<EditText
android:id="@+id/txtMessage"
android:layout_width="fill_parent"
android:layout_height="150px"
android:gravity="top"
/>
<Button
android:id="@+id/btnSendSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send SMS"
/>
</LinearLayout>
这是java文件(SMS.java): -
package net.learn2develop.SMSMessaging;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.telephony.SmsManager;
public class SMS extends Activity
{
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
txtMessage = (EditText) findViewById(R.id.txtMessage);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
if (phoneNo.length()>0 && message.length()>0) {
sendSMS(phoneNo, message);
} else {
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void sendSMS(String phoneNumber, String message)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, SMS.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
}
}
错误部分: -
08-26 23:06:41.040: ERROR/AndroidRuntime(411): FATAL EXCEPTION: main
08-26 23:06:41.040: ERROR/AndroidRuntime(411): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.learn2develop.SMSMessaging/net.learn2develop.SMSMessaging.SMS}: java.lang.NullPointerException
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at android.os.Looper.loop(Looper.java:123)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at java.lang.reflect.Method.invoke(Method.java:507)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at dalvik.system.NativeStart.main(Native Method)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): Caused by: java.lang.NullPointerException
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at net.learn2develop.SMSMessaging.SMS.onCreate(SMS.java:31)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-26 23:06:41.040: ERROR/AndroidRuntime(411): ... 11 more
答案 0 :(得分:1)
你在第31行得到一个NullPointerException,这是btnSendSMS.setOnClickListener(
可能没有找到带有id的视图。
作为解决方法添加
if (btnSendSMS != null) {
btnSendSMS.setOnClickLi......
}