首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓bindService抛出NullPointerException

安卓bindService抛出NullPointerException
EN

Stack Overflow用户
提问于 2014-08-25 09:10:51
回答 2查看 2.9K关注 0票数 1

我完成了几个教程,用安卓构建了我的第一个bindService,但是我一直在获取我不懂的NullPointerException。我的最后一个例子很简单。

MyActivity.java:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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 ..。

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-25 09:41:12

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

在代码行下面移动,

代码语言:javascript
复制
int r = mService.sum(1, 2);
tvResult.setText(new String().valueOf(r));

对于onServiceConnected()函数,如下所示

代码语言:javascript
复制
@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));
    }

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

票数 2
EN

Stack Overflow用户

发布于 2014-08-25 09:33:24

请检查活动生命周期。onCreate方法是在onStart方法之前调用的。您正在onStart中初始化onStart对象,并在onCreate中使用它。

参考文献:http://developer.android.com/reference/android/app/Activity.html

在onCreate as mService中,它不是初始化的,并且它在java中按对象的默认值是空的,它抛出空指针异常。

理想情况下,您应该从onServiceConnected回调调用sum方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25482480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档