我正在尝试实现一个信用卡模拟应用程序,并设置一个运行如预期的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>
并具有以下辅助. 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"
此时,NPE将显示在www.example.com中的第13行HceService.java:
File file = getFilesDir();
因此主要问题是:如何从服务或更好的HostApduService类访问文件?.是否有办法为服务提供非空上下文?
先谢了。
1条答案
按热度按时间kyvafyod1#
我在用行之有效的解决方案回答我自己的问题。
主要错误是尝试在服务的构造函数中获取上下文:
此时上下文还不可用,我收到了空指针异常。
解决方案是将代码从构造函数移动到onCreate-method:
这就是结果: