【问题标题】:Android: Unable to instantiate service Java.NullPointerExceptionAndroid:无法实例化服务 Java.NullPointerException
【发布时间】:2013-12-22 13:58:43
【问题描述】:

我对此感到非常愚蠢,看了多个教程和Stackoverflow问题,但仍然无法解决。

我正在编写一个非常简单的 Android mp3 Stream 监听器。我想使用服务,以便流可以在后台运行。但我无法启动服务:

12-22 08:42:58.738: W/dalvikvm(1701): threadid=1: 线程以未捕获的异常退出 (group=0xb3aa2ba8)

12-22 08:42:58.788: E/AndroidRuntime(1701): 致命异常: main 12-22 08:42:58.788: E/AndroidRuntime(1701): 进程: com.wankoradio, PID: 1701 12-22 08:42:58.788: E/AndroidRuntime(1701): java.lang.RuntimeException: 无法实例化服务 com.wankoradio.MediaPlayerService: java.lang.NullPointerException 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 android.app.ActivityThread.handleCreateService(ActivityThread.java:2556) 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 android.app.ActivityThread.access$1800(ActivityThread.java:135) 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 android.os.Handler.dispatchMessage(Handler.java:102) 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 android.os.Looper.loop(Looper.java:136) 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 android.app.ActivityThread.main(ActivityThread.java:5017) 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 java.lang.reflect.Method.invokeNative(Native Method) 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 java.lang.reflect.Method.invoke(Method.java:515) 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 dalvik.system.NativeStart.main(Native Method) 12-22 08:42:58.788: E/AndroidRuntime(1701): 引起: java.lang.NullPointerException 12-22 08:42:58.788: E/AndroidRuntime(1701): 在 android.content.ContextWrapper.getSystemService(ContextWrapper.java:540) 12-22 08:42:58.788: E/AndroidRuntime(1701): at com.wankoradio.MediaPlayerService.(MediaPlayerService.java:27)

我的 MainActivity 正在启动服务,看起来有点像这样:

public class MainActivity extends Activity implements OnClickListener {
private Button pausePlayButton;
private static String ACTION_PLAY = "com.action.PLAY";
private MediaPlayer mediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    pausePlayButton = (Button) findViewById(R.id.pausePlay_Button);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    pausePlayButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (!isPlaying) {
    pausePlayButton.setBackgroundResource(R.drawable.button_pause);
    
    Intent mServiceIntent = new Intent(getApplicationContext(), MediaPlayerService.class);    
    mServiceIntent.setAction(ACTION_PLAY);
    startService(mServiceIntent);

我的服务看起来像这样:

public class MediaPlayerService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {
    //Creating a matching Intent
    private static final String ACTION_PLAY = "com.action.PLAY";
    //Creating our main MediaPlayer
    MediaPlayer mMediaPlayer = null;
    //NotificationID
    private static final int NOTIFICATION_ID = 1234;
    
    //Wifi should not be shut down while our App is running, therefore we Lock it
    WifiLock wifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE))
            .createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");

    @Override
    public void onCreate() {
        super.onCreate();
        Notification notification = StartNotification();
        startForeground(NOTIFICATION_ID, notification);
    }
    
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent.getAction().equals(ACTION_PLAY)) {           
            //Wifi should not be shut down while our App is running, therefore we Lock it
            wifiLock.acquire();
            
            StartMediaPlayer();
        }                       
        return 0;
    }
    
    private Notification StartNotification() {
        String songName = "Woop";
        // assign the song name to songName
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
                     new Intent(getApplicationContext(), MainActivity.class),
                     PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new Notification();
        //notification.tickerText = text;
        //notification.icon = R.drawable.play0;
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
        notificationManager.notify(NOTIFICATION_ID, notification);
        return notification;
    }

我尝试调试它,但我的服务的 onCreate 或 onStartCommand 不会被调用

这是我的清单:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.wankoradio.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:enabled="true"
        android:name=".MediaPlayerService"
        android:label="MediaPlayer Service" >
        <intent-filter>
        <action
            android:name = "com.action.PLAY">
        </action>
        </intent-filter>
    </service>
    <receiver 
        android:name=".MainActivity">
        <intent-filter>
        <action
            android:name = "com.action.PLAY">
        </action>
        </intent-filter>
    </receiver>
</application>

对不起,如果我在浪费你的时间,这是我的第一个 Android 应用程序,在 WinPhone 上这当然更容易做到。

【问题讨论】:

    标签: java android android-intent


    【解决方案1】:
    WifiLock wifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE))
            .createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");
    

    系统服务在对象实例化时不可用。将您的 wifiLock 初始化推迟到 onCreate() 或更高版本。

    【讨论】:

    • 谢谢,我正在 atm 测试它,如果问题仍然存在,我会改变我的问题
    • 天哪,谢谢,这确实是问题所在。我更改了通知管理器和所有内容,因此稍后在 OnCreate 中,只是忘记了 WifiLock。我发现 catLog 很难阅读。将在 9 分钟内接受。
    猜你喜欢
    • 2011-06-28
    • 2014-07-27
    • 2021-11-13
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多