从活动A调用方法到活动B时出错

时间:2012-06-28 04:31:10

标签: android sms

当我收到短信(在两个模拟器上测试)时,我想在进度条中增加进度但是我收到错误 - 意外地,强行关闭。

这是我在活动A中的代码:

progressBar = (ProgressBar)findViewById(R.id.progresslevel);
  new Thread(new Runnable() {
    public void run() {
      // TODO Auto-generated method stub
      while (progressStatus < 50000) {
        mHandler.post(new Runnable() {
          public void run() {
            // TODO Auto-generated method stub
            progressBar.setProgress(progressStatus);
          }
        });
      }
  }).start();

增加活动A中的方法:

public static void increaseValue(Boolean isSMS) {
  if (isSMS == true) {
    progressStatus += 5000;
  } else {
    progressStatus += 7000;
  }
}

我的活动SMSReceiver:

public class SMSReceive extends BroadcastReceiver {
  @Override
    public void onReceive(Context context, Intent intent) {
      // TODO Auto-generated method stub
      FloCareActivity.increaseValue(true);
    }
}

如何解决?

log cat错误:

    06-28 11:24:33.733: W/dalvikvm(915): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
06-28 11:24:33.733: E/AndroidRuntime(915): Uncaught handler: thread main exiting due to uncaught exception
06-28 11:24:33.763: E/AndroidRuntime(915): java.lang.RuntimeException: Unable to instantiate receiver nth.flocare.demo.SMSReceiver: java.lang.ClassNotFoundException: nth.flocare.demo.SMSReceiver in loader dalvik.system.PathClassLoader@43757c18
06-28 11:24:33.763: E/AndroidRuntime(915):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2520)
06-28 11:24:33.763: E/AndroidRuntime(915):  at android.app.ActivityThread.access$3000(ActivityThread.java:116)
06-28 11:24:33.763: E/AndroidRuntime(915):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1843)
06-28 11:24:33.763: E/AndroidRuntime(915):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 11:24:33.763: E/AndroidRuntime(915):  at android.os.Looper.loop(Looper.java:123)
06-28 11:24:33.763: E/AndroidRuntime(915):  at android.app.ActivityThread.main(ActivityThread.java:4203)
06-28 11:24:33.763: E/AndroidRuntime(915):  at java.lang.reflect.Method.invokeNative(Native Method)
06-28 11:24:33.763: E/AndroidRuntime(915):  at java.lang.reflect.Method.invoke(Method.java:521)
06-28 11:24:33.763: E/AndroidRuntime(915):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
06-28 11:24:33.763: E/AndroidRuntime(915):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
06-28 11:24:33.763: E/AndroidRuntime(915):  at dalvik.system.NativeStart.main(Native Method)
06-28 11:24:33.763: E/AndroidRuntime(915): Caused by: java.lang.ClassNotFoundException: nth.flocare.demo.SMSReceiver in loader dalvik.system.PathClassLoader@43757c18
06-28 11:24:33.763: E/AndroidRuntime(915):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
06-28 11:24:33.763: E/AndroidRuntime(915):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
06-28 11:24:33.763: E/AndroidRuntime(915):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
06-28 11:24:33.763: E/AndroidRuntime(915):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2513)
06-28 11:24:33.763: E/AndroidRuntime(915):  ... 10 more
06-28 11:24:33.813: I/dalvikvm(915): threadid=7: reacting to signal 3
06-28 11:24:33.895: I/dalvikvm(915): Wrote stack trace to '/data/anr/traces.txt'
06-28 11:29:33.833: I/Process(915): Sending signal. PID: 915 SIG: 9

2 个答案:

答案 0 :(得分:2)

这意味着您的清单告诉Android您有一个名为nth.flocare.demo.SMSReceiver的类,Android无法找到它。

您所做的错误是

您在Code中的类名为SMSReceive,Android正在尝试查找nth.flocare.demo.SMSReceiver所以将您的班级重命名为SMSReceiver

答案 1 :(得分:0)

您的代码在Thread中创建了一个紧密的循环,您已尽可能快地向Runnable事件循环发布新的Handler。您还没有发布足够的代码或堆栈跟踪,以便我们查看这是否是唯一的问题,但肯定是需要修复的问题。仅在进度值更改时发布消息。当然,没有必要单独的线程来做到这一点。这是一个非常快速的操作。应该仅使用其他线程来防止UI线程被长时间操作延迟。