尝试在空对象引用(服务)上调用虚方法(Attempt to invoke virtual method on a null object reference (service))

编程入门 行业动态 更新时间:2024-10-20 05:42:10
尝试在空对象引用(服务)上调用虚方法(Attempt to invoke virtual method on a null object reference (service))

我一直得到同样的错误, 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

错误如下。

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

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

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

我被困在这几个小时,我找不到任何解决方案。 任何帮助,将不胜感激。 :d

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.

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.

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.

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

最满意答案

bindService()和startService()都是异步的。 在调用onServiceConnected()之前,您的mService对象将为null ,因此在此之前不能使用它。

Both bindService() and startService() are asynchronous. Your mService object will be null until onServiceConnected() is called, so you cannot use it until that point.

更多推荐

本文发布于:2023-07-07 21:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   方法   invoke   Attempt   virtual

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!