android致命异常:无法实例化活动

时间:2013-07-02 14:25:33

标签: android runtimeexception

我的android项目中有3个类:

  1. Launch.java
  2. SmsReceiver.java
  3. StartActivity.java
  4. Launch.java如下: -

    package alertApp;
    
    import com.alertapp.R;
    import android.app.Activity;
    import android.os.Bundle;
    
    public class Launch extends Activity {      
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.launcher);
            }
    
    }
    

    SmsReceiver类使用广播接收器来访问传入消息并向StartActivity类发出意图。

    编辑:这是SmsReceiver类:

        package alertApp;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.widget.Toast;
    
    
    
        public  class SmsReceiver extends BroadcastReceiver {
            //String number = "+919561356345";
            String number = "LM-WAYSMS";
            String sender, body;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Bundle extras = intent.getExtras();
            if(extras == null)
                return;
            Object[] pdus = (Object[]) extras.get("pdus");
             for (int i = 0; i <  pdus.length; i++) {
                 SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
                 sender = SMessage.getOriginatingAddress().toString();
                 body = SMessage.getMessageBody().toString();
                 Toast.makeText(context, "From : " +sender + "\n Message : " +body, Toast.LENGTH_LONG).show();
             }
    
             if(sender.contentEquals(number)){
                  Intent open = new Intent(context, StartActivity.class);
                  open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  open.putExtra("body", body);
                  open.putExtra("number", number);
                  context.startActivity(open);
             }
        }
    
    }
    

    编辑:更新了清单:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.alertapp"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />
     <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <activity
            android:name="com.alertapp.Launch"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name="com.alertapp.StartActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.testapp.NextActivity" />
    
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
         <receiver
            android:name="com.alertapp.SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS"
            android:enabled="true"> 
            <intent-filter>
                <action android:name = "android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>
    

    当我通过连接手机运行程序时,它显示致命异常错误。

        07-02 19:41:46.512: E/AndroidRuntime(10003): FATAL EXCEPTION: main
    07-02 19:41:46.512: E/AndroidRuntime(10003): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.alertapp/com.alertApp.Launch}: java.lang.ClassNotFoundException: com.alertApp.Launch
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at android.os.Handler.dispatchMessage(Handler.java:99)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at android.os.Looper.loop(Looper.java:137)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at android.app.ActivityThread.main(ActivityThread.java:4517)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at java.lang.reflect.Method.invokeNative(Native Method)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at java.lang.reflect.Method.invoke(Method.java:511)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at dalvik.system.NativeStart.main(Native Method)
    07-02 19:41:46.512: E/AndroidRuntime(10003): Caused by: java.lang.ClassNotFoundException: com.alertApp.Launch
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at android.app.Instrumentation.newActivity(Instrumentation.java:1027)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)
    07-02 19:41:46.512: E/AndroidRuntime(10003):    ... 11 more
    

1 个答案:

答案 0 :(得分:3)

这是Launch

的包
package alertApp;

并且已在xml中定义:

<activity
    android:name="com.alertApp.Launch"
    android:label="@string/app_name" >

您的活动定义中似乎包含错误的包名称。