Android bindService抛出NullPointerException
为了在Android中构建我的第一个bindService,我学习了好几本教程,但我一直得到同样的NullPointerException,我不明白。我的最后一个例子很简单 MyActivity.java:Android bindService抛出NullPointerException,android,service,nullpointerexception,Android,Service,Nullpointerexception,为了在Android中构建我的第一个bindService,我学习了好几本教程,但我一直得到同样的NullPointerException,我不明白。我的最后一个例子很简单 MyActivity.java: package de.123team.myapplication; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import androi
package de.123team.myapplication;
import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView;
public class MyActivity extends Activity {
// Connection
MySumService mService;
boolean mBound;
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
//mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBound = true;
MySumService.LocalBinder binder = (MySumService.LocalBinder)service;
mService = binder.getService();
}
};
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
if (mBound) {
mService.unbindService(mConnection);
mBound = false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
TextView tvResult = (TextView)findViewById(R.id.tvResult);
//tvResult.setText("Test123");
Intent i = new Intent(this, MySumService.class);
bindService(i, mConnection, BIND_AUTO_CREATE);
int r = mService.sum(1, 2);
tvResult.setText(new String().valueOf(r));
} }
MySumService.java:
package de.123team.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MySumService extends Service {
private IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return mBinder;
}
public int sum(int a, int b) {
return 666;
}
public class LocalBinder extends Binder {
public MySumService getService() {
return MySumService.this;
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.proteam.myapplication" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
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:name=".MySumService"
android:enabled="true"
android:exported="true" >
</service>
</application> </manifest>
请检查活动生命周期。onCreate方法在onStart方法之前调用。您正在onStart中初始化mService对象,并在onCreate中使用它 参考: 在onCreate as mService中,未初始化mService,根据java中对象的默认值,它为null,引发null指针异常
理想情况下,您应该从onServiceConnected回调调用sum方法。这里,您在调用bindservice()函数后立即调用sum方法 移动到代码行下面
int r = mService.sum(1, 2);
tvResult.setText(new String().valueOf(r));
要使用如下所示的OnServiceConnectiond()函数
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBound = true;
MySumService.LocalBinder binder = (MySumService.LocalBinder)service;
mService = binder.getService();
int r = mService.sum(1, 2);
tvResult.setText(new String().valueOf(r));
}
它将确保您的服务绑定到您的活动。MyActivity.java:86行?您的清单文件在哪里?您不能调用mService.sum(1,2);在onCreate中,mService尚未分配。您是对的。我将行从onStart移到onCreate,但结果是一样的。请不要使用>符号,而只是缩进4个空格以显示代码块。格式的详细信息可以在FAQ中找到:就像我在上面的评论中提到的一样-我解决了这个问题,但结果是一样的。
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBound = true;
MySumService.LocalBinder binder = (MySumService.LocalBinder)service;
mService = binder.getService();
int r = mService.sum(1, 2);
tvResult.setText(new String().valueOf(r));
}