努力让任何东西在android上的web3j中工作,尝试这个例子(我已经替换了token和addr)我使用的地址确实有一些rinkeby ETH。在手机上测试,当我加载这个类/活动时应用程序崩溃,代码在 oncreate 方法中。在清单中打开互联网权限,并在 build gradle 中编译 android web3j。
Web3j web3 = Web3jFactory.build(new
HttpService("https://rinkeby.infura.io/token"));
EthGetBalance ethGetBalance = null;
try {
ethGetBalance = web3
.ethGetBalance("addr",DefaultBlockParameterName.LATEST)
.sendAsync()
.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
BigInteger wei = ethGetBalance.getBalance();
错误
06-30 02:15:47.115 18904-18904/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.user.test, PID: 18904
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.user.test/com.test.user.test.balance}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2607)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5756)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.test.user.test.balance.onCreate(balance.java:43)
at android.app.Activity.performCreate(Activity.java:5619)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2512)
错误引用的第 43 行是我提交的代码中的最后一行代码。当我注释掉该行应用程序时,它不会崩溃并且只会收到一些(我假设是全部)警告。