0

我是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);
}

}

弹出消息

package com.example.popup;

import java.io.Serializable;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class PopMessage implements Serializable{
private String sender;
private String body;
private long timestamp;

public String getSender() {
    return sender;
}

public void setSender(String sender) {
    this.sender = sender;
}

public String getBody() {
    return body;
}

public void setBody(String body) {
    this.body = body;
}

public long getTimestamp() {
    return timestamp;
}

public void setTimestamp(long timestamp) {
    this.timestamp = timestamp;
}

public String getShortDate(long timestamp){
    Date date = new Date(timestamp);
    Calendar cal = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd hh:mmaa");
    sdf.setCalendar(cal);
    cal.setTime(date);
    return sdf.format(date);
}

}

这是日志

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
4

2 回答 2

1

您的PopUpAct课程扩展BroadcastReceiver了,这不是Activity. 因此,Android 会ClassCastException在启动您的活动时获得:

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

听起来您的AndroidManifest.xml文件表明这PopUpAct是一项活动,而您应该命名其他类。

于 2013-06-04T01:27:33.163 回答
0

使用 LogCat 窗口查看运行时错误发生的位置。它将是红色的。您可以通过使用“app:...”或“tag:...”来过滤要查看的日志信息。您可以使用 Log.e(tag, some_text_you_want_to_log) 在程序运行时将内容打印到日志中,以进行调试。

于 2013-06-04T01:15:33.837 回答