我一直在玩android,并遵循Text Message教程。 我输入的代码应该能够发送短信,但不能接收。 当我尝试运行应用程序时,它会在我看到屏幕之前安装,启动,变黑并崩溃。有谁知道什么是错的?此外,当我创建应用程序,非常简单的应用程序,只有一个按钮,它也安装,启动但崩溃。这是常见的,即使没有视觉错误吗?
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Message extends Activity {
Button sendSMS;
EditText msgTxt;
EditText numTxt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.sms.R.layout.message);
sendSMS = (Button) findViewById(com.example.sms.R.id.sendBtn);
msgTxt = (EditText) findViewById(com.example.sms.R.id.editText2);
numTxt = (EditText) findViewById(com.example.sms.R.id.numberTxt);
sendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String myMsg = msgTxt.getText().toString();
String theNumber = numTxt.getText().toString();
}
});
}
protected void sendMsg(String theNumber, String myMsg) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(theNumber, null, myMsg, null, null);
} }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<EditText
android:id="@+id/numberTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textMultiLine" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/sendBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name = "android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".myclass" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
03-16 18:09:41.669: D/AndroidRuntime(18171): Shutting down VM
03-16 18:09:41.669: W/dalvikvm(18171): threadid=1: thread exiting with uncaught
exception (group=0x419c12a0)
03-16 18:09:41.679: E/AndroidRuntime(18171): FATAL EXCEPTION: main
03-16 18:09:41.679: E/AndroidRuntime(18171):
java.lang.RuntimeException:
Unable to instantiate activity
{com.example.sms/com.example.sms.myclass}: java.lang.ClassNotFoundException:
com.example.sms.myclass
03-16 18:09:41.679: E/AndroidRuntime(18171): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2016)
03-16 18:09:41.679: E/AndroidRuntime(18171): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
03-16 18:09:41.679: E/AndroidRuntime(18171): at android.app.ActivityThread.access$700(ActivityThread.java:134)
03-16 18:09:41.679: E/AndroidRuntime(18171): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
03-16 18:09:41.679: E/AndroidRuntime(18171): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 18:09:41.679: E/AndroidRuntime(18171): at android.os.Looper.loop(Looper.java:137)
03-16 18:09:41.679: E/AndroidRuntime(18171): at android.app.ActivityThread.main(ActivityThread.java:4867)
03-16 18:09:41.679: E/AndroidRuntime(18171): at java.lang.reflect.Method.invokeNative(Native Method)
3-16 18:09:41.679: E/AndroidRuntime(18171): at java.lang.reflect.Method.invoke(Method.java:511)
03-16 18:09:41.679: E/AndroidRuntime(18171): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
03-16 18:09:41.679: E/AndroidRuntime(18171): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
03-16 18:09:41.679: E/AndroidRuntime(18171): at dalvik.system.NativeStart.main(Native Method)
03-16 18:09:41.679: E/AndroidRuntime(18171): Caused by: java.lang.ClassNotFoundException: com.example.sms.myclass
03-16 18:09:41.679: E/AndroidRuntime(18171): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
03-16 18:09:41.679: E/AndroidRuntime(18171): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-16 18:09:41.679: E/AndroidRuntime(18171): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-16 18:09:41.679: E/AndroidRuntime(18171): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
03-16 18:09:41.679: E/AndroidRuntime(18171): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2007)
03-16 18:09:41.679: E/AndroidRuntime(18171): ... 11 more
答案 0 :(得分:0)
您的班级名称为Message
而不是myclass
,将其更改为清单
这
<activity android:name=".myclass" android:label="@string/app_name" >
到
<activity android:name=".Message" android:label="@string/app_name" >
答案 1 :(得分:0)
您未在Activity
中正确指定Manifest
。您Activity
的名称为Message
,而非myclass
。改变这个:
<activity android:name=".myclass" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
到此:
<activity android:name=".Message" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>