java - 请有人帮助我,当我单击 Bind 并给我这个错误时,我的项目崩溃了,nullpointexception
问题描述
错误
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.Intent.getComponent()' on a null object reference
at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1725)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1904)
at android.app.ContextImpl.bindService(ContextImpl.java:1829)
at android.content.ContextWrapper.bindService(ContextWrapper.java:774)
at android.content.ContextWrapper.bindService(ContextWrapper.java:774)
at com.codekul.comman.MainActivity.lambda$onCreate$0$MainActivity(MainActivity.java:46)
at com.codekul.comman.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)
我的服务
package com.codekul.comman;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class MyService extends Service {
private MyImpl impl = new MyImpl();
@Override
public IBinder onBind(Intent intent) {
return impl;
}
private class MyImpl extends IComman.Stub{
@Override
public int calculate(int num1, int num2) throws RemoteException {
return num1 + num2;
}
}
}
这是我用来在客户端绑定主要活动的服务,使用aidl来通信服务器和客户端。
活动主体
<Button
android:id="@+id/btnBind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnBind" />
<Button
android:id="@+id/btnCalculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnCalculate"/>
当我单击第一个按钮时出现错误。
主要活动
package com.codekul.comman;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private IComman comman;
private final ServiceConnection serviceCon = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
comman = IComman.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnBind).setOnClickListener((view) -> {
Intent intent = new Intent("com.codekul.service.aidl");
bindService(convertImplicitToExplicitIntent(intent), serviceCon, BIND_AUTO_CREATE);
});
findViewById(R.id.btnCalculate).setOnClickListener((view) -> {
try {
Log.i("@codekul", "Addition is - " + comman.calculate(10, 100));
} catch (NullPointerException | RemoteException e) {
e.printStackTrace();
}
});
}
public Intent convertImplicitToExplicitIntent(Intent implicitIntent) {
PackageManager pm = getPackageManager();
@SuppressLint("QueryPermissionsNeeded") List<ResolveInfo> resolveInfoList = pm.queryIntentServices(implicitIntent, 0);
if (resolveInfoList == null || resolveInfoList.size() != 1) {
return null;
}
ResolveInfo serviceInfo = resolveInfoList.get(0);
ComponentName component = new ComponentName(serviceInfo.serviceInfo.packageName, serviceInfo.serviceInfo.name);
Intent explicitIntent = new Intent(implicitIntent);
explicitIntent.setComponent(component);
return explicitIntent;
}
}
这是我用来调用我的服务的代码,错误指向 binservice。任何人都可以帮助我解决这个错误。
解决方案
推荐阅读
- javascript - Angular Reactive Forms 多文件上传
- android - Android MPChart xAxis 偏移量
- reactjs - 应用状态改变时更新显示状态
- javascript - 为什么分号“意外”?
- reactjs - 如何从 create-react-app 项目中的代码渲染图像
- mysql - 如何手动将数据库从开发服务器同步到生产服务器
- nginx - 如何在 Kubernetes 托管应用程序中获取客户端 IP?
- winapi - 如何在 Win32 中读取鼠标的外观(指针、调整大小等)
- python - django编辑配置文件表单不保存
- python - numpy 获取在矩阵中创建数组的块