无法调用startService(intent)函数。 Intent是空指针异常

时间:2016-04-14 02:44:09

标签: android android-intent

我在我的app中添加了背景音乐。我有三个活动,所以我想设计一个由三个活动控制的音乐控制器类。但是当我在MainActivtiy中创建serviceIntent的对象并传递给MusicController类时,无法启动serviceIntent并且错误是空指针异常。有人怎么弄清楚这个问题?提前谢谢。

logcat的

04-13 22:30:15.189: D/AndroidRuntime(9581): Shutting down VM 04-13 22:30:15.190: E/AndroidRuntime(9581): FATAL EXCEPTION: main 04-13 22:30:15.190: E/AndroidRuntime(9581): Process: com.example.funwithmath, PID: 9581 04-13 22:30:15.190: E/AndroidRuntime(9581): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.funwithmath/com.example.funwithmath.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference 04-13 22:30:15.190: E/AndroidRuntime(9581): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 04-13 22:30:15.190: E/AndroidRuntime(9581): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 04-13 22:30:15.190: E/AndroidRuntime(9581): at android.app.ActivityThread.access$800(ActivityThread.java:151) 04-13 22:30:15.190: E/AndroidRuntime(9581): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 04-13 22:30:15.190: E/AndroidRuntime(9581): at android.os.Handler.dispatchMessage(Handler.java:102) 04-13 22:30:15.190: E/AndroidRuntime(9581): at android.os.Looper.loop(Looper.java:135) 04-13 22:30:15.190: E/AndroidRuntime(9581): at android.app.ActivityThread.main(ActivityThread.java:5254) 04-13 22:30:15.190: E/AndroidRuntime(9581): at java.lang.reflect.Method.invoke(Native Method) 04-13 22:30:15.190: E/AndroidRuntime(9581): at java.lang.reflect.Method.invoke(Method.java:372) 04-13 22:30:15.190: E/AndroidRuntime(9581): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 04-13 22:30:15.190: E/AndroidRuntime(9581): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 04-13 22:30:15.190: E/AndroidRuntime(9581): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference 04-13 22:30:15.190: E/AndroidRuntime(9581): at android.content.ContextWrapper.startService(ContextWrapper.java:516) 04-13 22:30:15.190: E/AndroidRuntime(9581): at com.example.funwithmath.util.MusicController.startMusic(MusicController.java:25) 04-13 22:30:15.190: E/AndroidRuntime(9581): at com.example.funwithmath.MainActivity.onCreate(MainActivity.java:58) 04-13 22:30:15.190: E/AndroidRuntime(9581): at android.app.Activity.performCreate(Activity.java:5990) 04-13 22:30:15.190: E/AndroidRuntime(9581): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 04-13 22:30:15.190: E/AndroidRuntime(9581): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 04-13 22:30:15.190: E/AndroidRuntime(9581): ... 10 more

MainActivity.java

public class MainActivity extends Activity {

private Button sound;
private Intent serviceIntent;
private MusicController musiControl;
private boolean musicPlayStatus = true;

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

    sound = (Button) findViewById(R.id.sound);
    sound.setBackgroundResource(R.drawable.soundopen);
    serviceIntent = new Intent(this, MusicServer.class);


    //Control Background Music
    musiControl = new MusicController (serviceIntent);
    musiControl.startMusic();

MusicController.java

public class MusicController extends Activity {

private boolean musicPlayStatus = true;
private Intent serviceIntent;

public MusicController(Intent serviceIntent) {
    // TODO Auto-generated constructor stub

    this.serviceIntent = serviceIntent;

}

public void startMusic() {
    // TODO Auto-generated method stub
    // Start Music

    startService(serviceIntent);
    musicPlayStatus = true;

}

2 个答案:

答案 0 :(得分:1)

您没有使用

分配对象

<强> MusicController.java

public class MusicController extends Activity {

private boolean musicPlayStatus = true;
private Intent serviceIntent;

public MusicController(Intent serviceIntent) {
    // TODO Auto-generated constructor stub

    this.serviceIntent = serviceIntent;

}

public void startMusic() {
    // TODO Auto-generated method stub
    // Start Music
serviceIntent= new Intent(MusicController.this, YourService.class);
    startService(serviceIntent);
    musicPlayStatus = true;

}

在这里,我可以向您展示启动和停止服务的简便方法

开始服务

startService(new Intent(this, MainService.class));

停止服务

stopService(new Intent(this, MainService.class));

注意: - 不要忘记在AndroidManifest中添加服务

<service android:enabled="true" android:name=".MainService" />

答案 1 :(得分:0)

此行告诉您需要知道的内容

尝试调用虚方法&#39; android.content.ComponentName android.content.Context.startService(android.content.Intent)&#39;在空对象引用上

基本上,MusicController类中没有上下文。该类需要知道启动服务的位置。我们可以通过传入mainActivity的上下文来解决这个问题。

public class MusicController extends Activity {

private boolean musicPlayStatus = true;
private Intent serviceIntent;
private Context context;

public MusicController(Context c, Intent serviceIntent) {
    // TODO Auto-generated constructor stub
    context = c;
    this.serviceIntent = serviceIntent;

}

public void startMusic() {

    context.startService(serviceIntent);
    musicPlayStatus = true;

}