我完成了一些教程,在Android中构建了我的第一个bindService,但我一直在获得同样的NullPointerException,我不明白。我的最后一个例子很简单...

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>


而我正在寻找令人沮丧的logCat数小时...

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)

最佳答案

在这里,您在调用bindservice()函数后立即调用sum方法。

移动到代码行下方,

int r = mService.sum(1, 2);
tvResult.setText(new String().valueOf(r));


如下所示的onServiceConnected()函数,

@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));
    }


这将确保您的服务绑定到您的活动。

10-04 10:54