空指针异常:尝试调用虚拟方法“void org.apache.cordova.CordovaPlugin.privateInitialize

dced5bon  于 2022-11-15  发布在  Apache
关注(0)|答案(2)|浏览(142)

我正在我的cordova插件做一些重构,我开始得到这个启动崩溃:

--------- beginning of crash
12-12 21:42:29.791  4693  4693 E AndroidRuntime: FATAL EXCEPTION: main
12-12 21:42:29.791  4693  4693 E AndroidRuntime: Process: xyz.meris.app, PID: 4693
12-12 21:42:29.791  4693  4693 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{xyz.meris.app/xyz.meris.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CordovaPlugin.privateInitialize(java.lang.String, org.apache.cordova.CordovaInterface, org.apache.cordova.CordovaWebView, org.apache.cordova.CordovaPreferences)' on a null object reference
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3162)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3305)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1993)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:106)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:216)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:7266)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
12-12 21:42:29.791  4693  4693 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CordovaPlugin.privateInitialize(java.lang.String, org.apache.cordova.CordovaInterface, org.apache.cordova.CordovaWebView, org.apache.cordova.CordovaPreferences)' on a null object reference
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.PluginManager.getPlugin(PluginManager.java:171)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.PluginManager.startupPlugins(PluginManager.java:97)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.PluginManager.init(PluginManager.java:86)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.CordovaWebViewImpl.init(CordovaWebViewImpl.java:117)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.CordovaActivity.init(CordovaActivity.java:149)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.CordovaActivity.loadUrl(CordovaActivity.java:224)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at xyz.meris.app.MainActivity.onCreate(MainActivity.java:39)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.Activity.performCreate(Activity.java:7353)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.Activity.performCreate(Activity.java:7344)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3142)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    ... 11 more
12-12 21:42:29.826  4693  4693 I Process : Sending signal. PID: 4693 SIG: 9

我四处搜寻,发现了一些类似的问题:

但这些解决方案对我不起作用...

ocebsuys

ocebsuys1#

我从其他问题中得到了一些线索,因为我正在重构,所以我一直在处理plugin.xml文件。
我注意到我与其他plugin.xml文件有如下区别:

<feature name="CDVProcessLog">
        <param name="android-package" value="xyz.meris.processlog.CDVProcessLog" />
        <param name="onload" value="true" />
      </feature>
      <feature name="ProcessLog">
        <param name="android-package" value="xyz.meris.processlog.ProcessLog" />
+       <param name="onload" value="true" />
      </feature>

因为我复制了<feature>标签,并且刚刚填充了它,所以我复制了onload参数。一旦我删除了重复的onload参数,崩溃就不再发生了。

**编辑:**erp.原来我有另一个错误导致了这个问题。

删除onload参数延迟加载了ProcessLog。所以我会得到一个空指针错误。
我真实的的问题是我的.js文件没有用我的CordovaPlugin类名更新。我已经把它从ProcessLog改为CDVProcessLogProcessLog现在有了处理日志的细节,CDVProcessLog主要有cordova胶水来调用我在ProcessLog中的方法。
我的.js文件需要更改:

return new Promise((resolve, reject) => {
-    exec(resolve, reject, 'ProcessLog', func, [...args]);
+    exec(resolve, reject, 'CDVProcessLog', func, [...args]);
  });
gcuhipw9

gcuhipw92#

我在一个Ionic应用程序中遇到了同样的问题。这个问题是由于platforms/android/app/src/main/java中的一些插件的config.xml中缺少Java包引起的。通常原因是手动添加/删除cordova插件。
要解决此问题,请查看config.xml文件中所有<feature>标记的列表,并确保platforms/android/app/src/main/java中存在其name属性中定义的包(文件夹和文件)。

<feature name="Device">
    <param name="android-package" value="org.apache.cordova.device.Device" />
</feature>
<feature name="SplashScreen">
    <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
    <param name="onload" value="true" />
</feature>
<!-- etc. -->

为了使搜索更容易,请在Android应用的日志文件中查找ClassNotFoundException

W/System.err: java.lang.ClassNotFoundException: org.apache.cordova.file.FileUtils

相关问题