应用程序意外停止,请在android中重试

应用程序意外停止,请在android中重试,android,Android,我是android平台的新手。我有个问题。运行应用程序时,出现错误“应用程序意外停止” 我尝试了一些解决方案,但我不明白,它仍然不起作用 有人能帮我吗 我的代码 package com.example.popup; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import an

我是android平台的新手。我有个问题。运行应用程序时,出现错误“应用程序意外停止”

我尝试了一些解决方案,但我不明白,它仍然不起作用

有人能帮我吗

我的代码

package com.example.popup;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;


public class PopSMSActivity extends Activity {
    @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       // no need for XML layouts right now
       // we will use a dialog instead
       //setContentView(R.layout.main); 

       // retrieve Serializable sms message object
       // by the key "msg" used to pass it
       Intent in = this.getIntent();
       PopMessage msg = (PopMessage) in.getSerializableExtra("msg");

       // Case where we launch the app to test the UI
       // i.e. no incoming SMS
       if(msg == null){
              msg = new PopMessage(); 
              msg.setSender("0123456789"); 
              msg.setTimestamp( System.currentTimeMillis() ); 
              msg.setBody(" this is a test SMS message!");
       }
       showDialog(msg);
}

private void showDialog(PopMessage msg){

    final String sender = msg.getSender();
    final String body = msg.getBody();

    final String display = sender + "\n"
            + msg.getShortDate( msg.getTimestamp() )+ "\n"
            + body + "\n";

    // Display in Alert Dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(display)
    .setCancelable(false)
    .setPositiveButton("Reply", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
                  // reply by calling SMS program
             // smsReply(sender, body);
        }
    })
    .setNegativeButton("Close", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
                  // go back to the phone home screen
               //   goHome();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}
}

弹幕

package com.example.popup;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;

public class PopUpAct extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle bundle = intent.getExtras();

    //get the sms received
    Object[] pdus = (Object[]) bundle.get("pdus");
    SmsMessage[] msgs = new SmsMessage[pdus.length];

    String smsSender = "";

    String smsBody = "";

    long timestamp = 0L;

    for (int i=0; i<msgs.length;i++){
        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
        smsSender += msgs[i].getOriginatingAddress();
        smsBody += msgs[i].getMessageBody().toString();
        timestamp += msgs[i].getTimestampMillis();
    }

    intent.putExtra("sender",smsSender);
    intent.putExtra("body", smsBody);
    intent.putExtra("timestamp",timestamp);


    PopMessage pop_msg = new PopMessage();

    intent.setClass(context, PopSMSActivity.class);
    intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);

    intent.putExtra("msg", pop_msg);
    context.startActivity(intent);
}

}
这是原木

06-04 08:14:38.387: E/AndroidRuntime(285): FATAL EXCEPTION: main
06-04 08:14:38.387: E/AndroidRuntime(285): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.popup/com.example.popup.PopUpAct}: java.lang.ClassCastException: com.example.popup.PopUpAct
06-04 08:14:38.387: E/AndroidRuntime(285):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
06-04 08:14:38.387: E/AndroidRuntime(285):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-04 08:14:38.387: E/AndroidRuntime(285):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-04 08:14:38.387: E/AndroidRuntime(285):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-04 08:14:38.387: E/AndroidRuntime(285):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 08:14:38.387: E/AndroidRuntime(285):  at android.os.Looper.loop(Looper.java:123)
06-04 08:14:38.387: E/AndroidRuntime(285):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-04 08:14:38.387: E/AndroidRuntime(285):  at java.lang.reflect.Method.invokeNative(Native Method)
06-04 08:14:38.387: E/AndroidRuntime(285):  at java.lang.reflect.Method.invoke(Method.java:521)
06-04 08:14:38.387: E/AndroidRuntime(285):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-04 08:14:38.387: E/AndroidRuntime(285):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-04 08:14:38.387: E/AndroidRuntime(285):  at dalvik.system.NativeStart.main(Native Method)
06-04 08:14:38.387: E/AndroidRuntime(285): Caused by: java.lang.ClassCastException: com.example.popup.PopUpAct
06-04 08:14:38.387: E/AndroidRuntime(285):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-04 08:14:38.387: E/AndroidRuntime(285):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
06-04 08:14:38.387: E/AndroidRuntime(285):  ... 11 more

使用LogCat窗口查看发生运行时错误的位置。它将是红色的。您可以使用“app:…”或“tag:…”筛选要查看的日志信息。您可以使用Log.e(标记,一些您想要的文本)在程序运行时打印到日志中进行调试。

您的
PopUpAct
类扩展了
BroadcastReceiver
,这不是
活动。因此,Android在启动您的活动时会得到一个
ClassCastException

06-04 08:14:38.387: E/AndroidRuntime(285): Caused by: java.lang.ClassCastException: com.example.popup.PopUpAct

听起来像是您的
AndroidManifest.xml
文件声明当您应该命名其他类时,
PopUpAct
是一个活动。

您的应用程序出现运行时错误,检查logcat并发布导致应用程序崩溃的代码。您将学习的重要技能之一是如何找到在哪里可以看到有关崩溃的更多信息。在你这么做之前,恐怕没有其他人能帮助你。我支持这一点,这可能会有所帮助
06-04 08:14:38.387: E/AndroidRuntime(285): Caused by: java.lang.ClassCastException: com.example.popup.PopUpAct