【问题标题】:How to connect an intent in the manifest to a Inner Class?如何将清单中的意图连接到内部类?
【发布时间】:2015-03-11 11:20:19
【问题描述】:

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

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]]

怎么办?

【问题讨论】:

  • 因为你的内部类不是静态的

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


【解决方案1】:

变化:

public class MusicIntentReceiver extends BroadcastReceiver

到:

public static class MusicIntentReceiver extends BroadcastReceiver

否则,除了Fragment_playMedias 之外,没有人可以创建MusicIntentReceiver 的实例。

【讨论】:

  • 谢谢,但坏消息!这将导致课堂内的许多其他问题!好的,我会尝试用不同的方式来做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-21
相关资源
最近更新 更多