我正在尝试实现一个信用卡模拟应用程序并设置一个按预期运行的 HostApduService 类。
现在我正在尝试从文件(可以在内部或外部存储、固定名称和文件夹中)加载卡数据,以便更灵活地处理我要模拟的卡数据。
尽管 HostApduService 是一项具有上下文的服务我无法从该服务访问文件 - 我总是收到“java.lang.NullPointerException”:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.androidcrypto.hcecreditcardemulator, PID: 26574
java.lang.RuntimeException: Unable to instantiate service de.androidcrypto.hcecreditcardemulator.HceService: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3529)
at android.app.ActivityThread.-wrap4(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:237)
at de.androidcrypto.hcecreditcardemulator.HceService.<init>(HceService.java:13)
at java.lang.Class.newInstance(Native Method)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3526)
at android.app.ActivityThread.-wrap4(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
编辑 3 月 15 日: 我检查了
getBaseContext()
返回 null
但是 getApplicationContext()
强制了 NullPointerException
。因此,getFiles()
也因 NPE 而失败。
这是我正在使用的(非常短的)HceService.java 类:
package de.androidcrypto.hcecreditcardemulator;
import android.nfc.cardemulation.HostApduService;
import android.os.Bundle;
import java.io.File;
public class HceService extends HostApduService {
public HceService() {
// check for a specific file in internal storage
System.out.println("start HceService");
File file = getFilesDir();
//File file2 = getExternalFilesDir(null);
System.out.println("file exists: " + file.exists());
}
@Override
public byte[] processCommandApdu(byte[] bytes, Bundle bundle) {
return new byte[0];
}
@Override
public void onDeactivated(int i) {
}
}
会从AndroidManifest.xml开始:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<!-- Card emulation was introduced in API 19. -->
<!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->
<uses-feature android:name="android.hardware.nfc.hce" android:required="true" />
<uses-permission android:name="android.permission.NFC" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.HceCreditCardEmulator"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- BEGIN_INCLUDE(CardEmulationManifest) -->
<!-- Service for handling communication with NFC terminal. -->
<service android:name=".HceService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE">
<!-- Intent filter indicating that we support card emulation. -->
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<!-- Required XML configuration file, listing the AIDs that we are emulating cards
for. This defines what protocols our card emulation service supports. -->
<meta-data android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="@xml/aid_list"/>
</service>
<!-- END_INCLUDE(CardEmulationManifest) -->
</application>
</manifest>
并有这个 aid.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<aid-group android:description="@string/card_title" android:category="payment">
<aid-filter android:name="F222222222"/>
<aid-filter android:name="325041592E5359532E4444463031"/>
<aid-filter android:name="A0000000031010"/>
</aid-group>
<!-- END_INCLUDE(CardEmulationXML) -->
</host-apdu-service>
由于它具有“付款”类别,因此可以在设备的设置中轻松切换服务:
Settings -> connections -> NFC -> Tap and pay -> tab "payments" select "HCE"
此时在HceService.java中的这一行13处抛出了NPE:
File file = getFilesDir();
所以主要问题是:如何从服务或更好的 HostApduService 类访问文件?。有没有办法为服务提供非空上下文?
提前致谢。
我正在用有效的解决方案回答我自己的问题。
主要错误是试图在服务的构造函数中获取上下文:
public HceService() {
// check for a specific file in internal storage
System.out.println("start HceService");
System.out.println("getBaseContext: " + getBaseContext());
System.out.println("getApplicationContext: " + getApplicationContext());
File file = getFilesDir();
//File file2 = getExternalFilesDir(null);
System.out.println("file exists: " + file.exists());
}
此时上下文还不可用,收到空指针异常
解决方案是将代码从构造函数移动到onCreate-method:
public HceService() {
/*
// move these lines to onCreate method
// check for a specific file in internal storage
System.out.println("start HceService");
System.out.println("getBaseContext: " + getBaseContext());
System.out.println("getApplicationContext: " + getApplicationContext());
File file = getFilesDir();
//File file2 = getExternalFilesDir(null);
System.out.println("file exists: " + file.exists());
*/
}
@Override
public void onCreate() {
super.onCreate();
// check for a specific file in internal storage
System.out.println("start HceService");
System.out.println("getBaseContext: " + getBaseContext());
System.out.println("getApplicationContext: " + getApplicationContext());
File file = getFilesDir();
//File file2 = getExternalFilesDir(null);
System.out.println("file exists: " + file.exists());
}
这是结果:
I/System.out: start HceService
I/System.out: getBaseContext: android.app.ContextImpl@ecd5e6
I/System.out: getApplicationContext: android.app.Application@3e75727
I/System.out: file exists: true