如何将清单中的意图连接到内部类?

时间:2015-03-11 11:20:20

标签: android android-intent android-fragments android-manifest

我尝试将AUDIO_BECOME_NOISY连接到片段中的Receiver:

public class Fragment_playMedias extends Fragment  {

    ...

    public class MusicIntentReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            ...
        }
   }
}

我的清单包括:

    <receiver android:name=".Fragment_playMedias$MusicIntentReceiver" >
        <intent-filter>
            <action android:name="android.media.AUDIO_BECOMING_NOISY" />
        </intent-filter>
    </receiver>

因此,当我拔下耳机时,我收到以下消息:

E/AndroidRuntime(848): java.lang.RuntimeException: Unable to instantiate receiver com.blah.Fragment_playMedias$MusicIntentReceiver: java.lang.InstantiationException: class com.blah.Fragment_playMedias$MusicIntentReceiver has no zero argument constructor

对于内部类来说似乎特别重要,因为如果我在自己的文件中以完全相同的方式声明MusicIntentReceiver,则没有问题!
 它可能与here相关,但我真的不明白如何解决我的问题!

我尝试插入一个空构造函数:

    public MusicIntentReceiver() {

    }

这样我收到了:

E/AndroidRuntime(3047): java.lang.RuntimeException: Unable to instantiate receiver com.cabbonline.ndguide.Fragment_plyMedias$MusicIntentReceiver: java.lang.ClassNotFoundException: Didn't find class "com.cabbonline.ndguide.Fragment_plyMedias$MusicIntentReceiver" on path: DexPathList[[zip file "/data/app/com.cabbonline.ndguide-1/base.apk"],nativeLibraryDirectories=[/data/app/com.cabbonline.ndguide-1/lib/arm, /vendor/lib, /system/lib]]

怎么办?

1 个答案:

答案 0 :(得分:1)

变化:

public class MusicIntentReceiver extends BroadcastReceiver

为:

public static class MusicIntentReceiver extends BroadcastReceiver

否则,Fragment_playMedias以外的任何人都无法创建MusicIntentReceiver的实例。