gpt4 book ai didi

java - bindService 上的 NullPointerException getPackageName()

转载 作者:行者123 更新时间:2023-11-30 05:02:44 33 4
gpt4 key购买 nike

我正在尝试为 Android 应用程序创建远程服务。我或多或少地从 Android Developers 网站复制了代码,并根据我的需要对其进行了重组。但是,当我进入 bindService 步骤时,我在 android.content.Context.getPackageName() 上得到了 NullPointerException。我做错了什么?

我在较早版本的应用程序中使用了进程中服务,但这还不足以满足我的需求。除了必要的代码更改之外,服务本身在两者之间保持不变。我查看了此处的所有结果,但它们要么不相关,要么对我没有任何帮助。

我已经尝试将调用从 onCreate 转移到 onStart。没有不同。 BinderActivity 的 onCreate 从未执行过。我尝试了 startService 而不是 bindService,但它给了我同样的错误。我已经检查过 this、intent 和 mConnection 都是非空的(它们是)。我知道该服务没有启动,但我正在使用 Context.BIND_AUTO_CREATE(如果没有启动它应该启动它)。除了在这个问题上挥舞一只死鸡,我不确定在这里还能做什么。

我的主要 Activity :

package com.example.trainchecker;

import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;


import android.os.Message;
import android.os.Messenger;

import java.io.*;
import java.util.ArrayList;


import android.widget.TextView;


public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BinderActivity ba = new BinderActivity();
ba.BindService();
}
}

这是 BinderActivity,魔术应该在这里发生。相反,错误确实是:

package com.example.trainchecker;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.os.Message;
import android.os.Handler;
import android.os.Messenger;



public class BinderActivity extends Activity {
public Messenger ms = null;
private MainActivity ma;

class IncomingHandler extends Handler{
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);
}
}

final Messenger mm = new Messenger(new IncomingHandler());

private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.d("MCon","Sentinel Online");
ms = new Messenger(service);
try {
Message msg = Message.obtain(null,TCService.MSG_REGISTER_CLIENT);
msg.replyTo = mm;
ms.send(msg);
} catch (RemoteException e){ e.printStackTrace();}
}
public void onServiceDisconnected(ComponentName className) {
Log.d("MCon","Sentinel Offline");
try {
Message msg = Message.obtain(null,2);
msg.replyTo = mm;
ms.send(msg);
} catch (RemoteException e){ e.printStackTrace();}
}
};



public void BindService() {
bindService(new Intent(this, TCService.class), mConnection, Context.BIND_AUTO_CREATE);
}


}


最后,服务本身。那里不多,但它是我的:

package com.example.trainchecker;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import java.util.ArrayList;
import android.os.Message;
import android.os.Messenger;

public class TCService extends Service {

ArrayList<Messenger> mc = new ArrayList<>();

@Override
public IBinder onBind(Intent i) {
Log.d("TCS", "Binding Now");
return mm.getBinder();
}


static final int MSG_REGISTER_CLIENT = 1;

class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
mc.add(msg.replyTo);
break;
case 2:
mc.remove(msg.replyTo);
break;
default:
super.handleMessage(msg);
}
}
}

final Messenger mm = new Messenger(new IncomingHandler());
}

这里是完整的错误:

09-15 11:39:04.518 15448-15448/com.example.trainchecker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.trainchecker, PID: 15448
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trainchecker/com.example.trainchecker.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133)
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:4468)
at com.example.trainchecker.BinderActivity.BindService(BinderActivity.java:58)
at com.example.trainchecker.MainActivity.onCreate(MainActivity.java:65)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.access$900(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:171) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

最佳答案

以编程方式实例化 Activity 实例不会触发生命周期,因此您的上下文在那里无效。将其传递给您的 BindService() 调用。从技术上讲,您的 BinderActivity 不需要存在并且可以存在于您的 MainAcitivity 或服务中。

关于java - bindService 上的 NullPointerException getPackageName(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57946081/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com