0

我设置

public class MainActivity extends Activity {

 Button btn1, btn2;
 private String data;
 private EditText e;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  requestWindowFeature(Window.FEATURE_PROGRESS);
  setContentView(R.layout.activity_main);
  setProgressBarVisibility(true);
 }
}

在我的 MainActivity 中。我想在另一个扩展广播接收器类的类中停止进度条活动。

public class Serious extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
  ((Activity) context).setProgressBarVisibility(false);
 }
}

当我试图停止严重类中的进度条时,出现以下异常

07-01 05:52:20.376: E/AndroidRuntime(2094): FATAL EXCEPTION: main
07-01 05:52:20.376: E/AndroidRuntime(2094): java.lang.RuntimeException: Unable to start receiver com.fivey.seriousandroiddevgcm.Serious: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to android.app.Activity
07-01 05:52:20.376: E/AndroidRuntime(2094):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2383)
07-01 05:52:20.376: E/AndroidRuntime(2094):     at android.app.ActivityThread.access$1500(ActivityThread.java:141)
07-01 05:52:20.376: E/AndroidRuntime(2094):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
07-01 05:52:20.376: E/AndroidRuntime(2094):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 05:52:20.376: E/AndroidRuntime(2094):     at android.os.Looper.loop(Looper.java:137)
07-01 05:52:20.376: E/AndroidRuntime(2094):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-01 05:52:20.376: E/AndroidRuntime(2094):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 05:52:20.376: E/AndroidRuntime(2094):     at java.lang.reflect.Method.invoke(Method.java:511)
07-01 05:52:20.376: E/AndroidRuntime(2094):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-01 05:52:20.376: E/AndroidRuntime(2094):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-01 05:52:20.376: E/AndroidRuntime(2094):     at dalvik.system.NativeStart.main(Native Method)
07-01 05:52:20.376: E/AndroidRuntime(2094): Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to android.app.Activity
07-01 05:52:20.376: E/AndroidRuntime(2094):     at com.fivey.seriousandroiddevgcm.Serious.onReceive(Serious.java:57)
07-01 05:52:20.376: E/AndroidRuntime(2094):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2376)
07-01 05:52:20.376: E/AndroidRuntime(2094):     ... 10 more

你能帮我解决这个问题吗?

4

1 回答 1

3

context您在接收器中收到的不是活动

让 Receiver 持有对 Activity 的引用,或者让 Receiver 成为 Activity 的内部类。

于 2013-07-01T05:57:35.180 回答