简体   繁体   English

尝试在空对象引用(服务)上调用虚拟方法

[英]Attempt to invoke virtual method on a null object reference (service)

I keep getting the same error, Unable to start activity ... java.lang.NullPointerException: Attempt to invoke virtual method ... on a null object reference , every time I try to invoke a function in a service from an activity. 我不断收到相同的错误, Unable to start activity ... java.lang.NullPointerException: Attempt to invoke virtual method ... on a null object reference每次尝试从活动中调用服务中的函数时,尝试Unable to start activity ... java.lang.NullPointerException: Attempt to invoke virtual method ... on a null object reference

The error is below. 错误如下。

10-18 22:30:46.371 1773-1773/com.example.test I/art: Not late-enabling -Xcheck:jni (already on)
10-18 22:30:47.054 1773-1785/com.example.test I/art: Background sticky concurrent mark sweep GC freed 2727(238KB) AllocSpace objects, 0(0B) LOS objects, 17% free, 939KB/1135KB, paused 19.920ms total 60.374ms
10-18 22:30:47.070 1773-1773/com.example.test D/AndroidRuntime: Shutting down VM
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: FATAL EXCEPTION: main
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: Process: com.example.test, PID: 1773
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.test.MyService.getRandomNumber()' on a null object reference
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:144)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5221)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.test.MyService.getRandomNumber()' on a null object reference
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at com.example.test.MainActivity.onCreate(MainActivity.java:27)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5937)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:144) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5221) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
10-18 22:30:58.000 1773-1780/com.example.test W/art: Suspending all threads took: 12.169ms
10-18 22:31:27.545 1773-1780/com.example.test W/art: Suspending all threads took: 17.580ms
10-18 22:31:31.555 1773-1780/com.example.test W/art: Suspending all threads took: 18.440ms
10-18 22:31:39.070 1773-1780/com.example.test W/art: Suspending all threads took: 11.531ms
10-18 22:31:51.100 1773-1780/com.example.test W/art: Suspending all threads took: 5.540ms

The MainActivity is below. MainActivity在下面。

package com.example.test;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    MyService mService;
    boolean mBound = false;

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

        // Bind this and the service together
        Intent noiseIntent = new Intent(this, MyService.class);
        bindService(noiseIntent, mConnection, Context.BIND_AUTO_CREATE);
        startService(noiseIntent);

        int num = mService.getRandomNumber();
        Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
    }

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            MyService.ServiceBinder binder = (MyService.ServiceBinder) service;
            mService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };
}

And the MyService is below. MyService在下面。

package com.example.test;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import java.util.Random;

public class MyService extends Service {
    // Binder given to clients
    private final IBinder mBinder = new ServiceBinder();
    // Random number generator
    private final Random mGenerator = new Random();

    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class ServiceBinder extends Binder {
        MyService getService() {
            // Return this instance of LocalService so clients can call public methods
            return MyService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /**
     * method for clients
     */
    public int getRandomNumber() {
        return mGenerator.nextInt(100);
    }
}

I've been stuck on this for a couple of hours and I couldn't find any solutions. 我已经坚持了几个小时,但找不到任何解决方案。 Any help would be appreciated. 任何帮助,将不胜感激。 :D :D

Both bindService() and startService() are asynchronous. bindService()startService()都是异步的。 Your mService object will be null until onServiceConnected() is called, so you cannot use it until that point. 在调用onServiceConnected()之前,您的mService对象将为null ,因此直到onServiceConnected()为止您都无法使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM