I worked through several tutorials to build my first bindService in Android but I keep on getting the same NullPointerException I don't understand. My last example is very simple...
MyActivity.java:
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>
And the depressing logCat I'm looking at for hours...
08-25 10:19:59.809 5107-5107/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.123team.myapplication/de.123team.myapplication.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at de.123team.myapplication.MyActivity.onCreate(MyActivity.java:86)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Here you are invoking the method sum immediately after calling bindservice() function.
Move below lines of code,
int r = mService.sum(1, 2);
tvResult.setText(new String().valueOf(r));
to onServiceConnected() function like below,
@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));
}
It will make sure that your service is bound to your activity.
Please check activity lifecycle. onCreate method is called before onStart method. You are initializing mService object in onStart and using it in onCreate.
Ref: http://developer.android.com/reference/android/app/Activity.html
In onCreate as mService is not initialized and it is null as per default value of object in java, its throwing null pointer exception.
Ideally, you should call sum method from onServiceConnected callback.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.