java.lang.RuntimeException:无法使用BroadcastReceiver内部类

时间:2017-02-26 15:06:51

标签: android broadcastreceiver runtimeexception

我已经阅读了有关此问题的先前主题,但答案无法解决我的问题。 我有一个BroadcastReceiver,它是MainActivity内部的一个内部类,它失败并出现以下异常:

  

java.lang.RuntimeException:无法实例化接收器com.tac.kulik.dealsgrabber.MainActivity $ DatabaseBroadcastReceiver:java.lang.InstantiationException:无法实例化类com.tac.kulik.dealsgrabber.MainActivity $ DatabaseBroadcastReceiver;没有空的构造函数                         在android.app.ActivityThread.handleReceiver(ActivityThread.java:2410)                         在android.app.ActivityThread.access $ 1700(ActivityThread.java:142)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1279)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:136)                         在android.app.ActivityThread.main(ActivityThread.java:5118)                         at java.lang.reflect.Method.invokeNative(Native Method)                         在java.lang.reflect.Method.invoke(Method.java:515)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:794)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)                         at dalvik.system.NativeStart.main(Native Method)                      引起:java.lang.InstantiationException:无法实例化类com.tac.kulik.dealsgrabber.MainActivity $ DatabaseBroadcastReceiver;没有空的构造函数                         at java.lang.Class.newInstanceImpl(Native Method)                         在java.lang.Class.newInstance(Class.java:1214)                         在android.app.ActivityThread.handleReceiver(ActivityThread.java:2405)                         在android.app.ActivityThread.access $ 1700(ActivityThread.java:142)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1279)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:136)                         在android.app.ActivityThread.main(ActivityThread.java:5118)                         at java.lang.reflect.Method.invokeNative(Native Method)                         在java.lang.reflect.Method.invoke(Method.java:515)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:794)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)                         在dalvik.system.NativeStart.main(本地方法)

使BroadcastReceiver成为静态不是一个选项,因为我无法访问我需要使用的必要成员变量。

这是MainRectivity与BroadcastReceiver内部类:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    .
    .
    .
    .
    .

    try {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent dbIntent = new Intent(MainActivity.this, DatabaseBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, dbIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmManager.INTERVAL_DAY, pendingIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public class DatabaseBroadcastReceiver extends BroadcastReceiver {

    public DatabaseBroadcastReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        final Context mContext = context;

        .
        .
        .
        .
        .

            @Override
            public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
                Log.d(TAG, "onLoadFinished s=" + cursor.getCount());

                mDealsAdapter.swapCursor(cursor);
            }

            @Override
            public void onLoaderReset(Loader<Cursor> arg0) {
                Log.d(TAG, "onLoaderReset");
                mDealsAdapter.swapCursor(null);
            }
        };
        getSupportLoaderManager().initLoader(0, new Bundle(), mCallback);
    }
};

的AndroidManifest.xml

<receiver android:name=".MainActivity$DatabaseBroadcastReceiver"/>

请告知任何人。

1 个答案:

答案 0 :(得分:1)

  

使BroadcastReceiver成为静态不是一个选项,因为我无法访问我需要使用的必要成员变量。

然后,这是您应用中的错误,您需要修复该错误。

当警报响起时,您的整个进程,更不用说活动了,很有可能。您的BroadcastReceiver需要能够完全独立于活动而工作。通常,这样的接收器实现为顶级public Java类,以帮助实现这种独立性。