gpt4 book ai didi

来自 DroidGap > onCreate > setWebChromeClient 和 ProgressBar 实现的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 18:48:25 25 4
gpt4 key购买 nike

距离上一次 Java 课已经过去好几年了,更不用说在工作中从未编写过 JAVA 了。

无论如何,我试图让这个 Cordova 应用程序在执行 javascript html + 初始 AJAX 调用时在启动屏幕上呈现加载小部件。

我已采纳 this post 的建议但我没有做正确的事情... appView 存在于 DroidGap.java源,因此不能为空。事实上,我不知道这里指向 null 的是什么。也许我这段代码放错了地方?

希望我有更多的时间来深入研究这里的事情......如果有人有一些建议,我会洗耳恭听,无论如何,我都会洗耳恭听。

就是这个pastebin post这看起来与上面的相同,所以我考虑过在 onCreate 之外添加进度条代码是没有用的。

package com.Project.MainActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;

import org.apache.cordova.*;

public class MainActivity extends DroidGap
{
final Activity activity = this;
private ProgressBar progessBar1;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

View footer = View.inflate(getContext(), R.layout.main, root);
root.addView(footer);
progessBar1 = (ProgressBar) findViewById(R.id.progressBar1);
this.appView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progessBar1.setProgress(progress * 1000);
if(progress < 100 && progessBar1.getVisibility() == ProgressBar.GONE) {
progessBar1.setVisibility(ProgressBar.VISIBLE);
}
progessBar1.setProgress(progress);
if(progress == 100) {
progessBar1.setVisibility(ProgressBar.GONE);
}

Log.d("Progress", progress+"");

}
});

super.setStringProperty("loadingDialog", "Wait, Loading...");
// Set by <content src="index.html" /> in config.xml

super.loadUrl("file:///android_asset/www/index.html");
}
}

此代码产生输出:

05-22 10:13:27.238: E/UpdateReceiver(11872): ACTION_PACKAGE_REMOVED
05-22 10:13:29.988: E/dalvikvm(14200): could not disable core file generation for pid 14200: Operation not permitted
05-22 09:37:01.394: E/AndroidRuntime(13370): FATAL EXCEPTION: main
05-22 09:37:01.394: E/AndroidRuntime(13370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Project.MainActivity/com.Project.MainActivity.MainActivity}: java.lang.NullPointerException
05-22 09:37:01.394: E/AndroidRuntime(13370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1702)
05-22 09:37:01.394: E/AndroidRuntime(13370): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1722)
05-22 09:37:01.394: E/AndroidRuntime(13370): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
05-22 09:37:01.394: E/AndroidRuntime(13370): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:974)
05-22 09:37:01.394: E/AndroidRuntime(13370): at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 09:37:01.394: E/AndroidRuntime(13370): at android.os.Looper.loop(Looper.java:130)
05-22 09:37:01.394: E/AndroidRuntime(13370): at android.app.ActivityThread.main(ActivityThread.java:3821)
05-22 09:37:01.394: E/AndroidRuntime(13370): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 09:37:01.394: E/AndroidRuntime(13370): at java.lang.reflect.Method.invoke(Method.java:507)
05-22 09:37:01.394: E/AndroidRuntime(13370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-22 09:37:01.394: E/AndroidRuntime(13370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-22 09:37:01.394: E/AndroidRuntime(13370): at dalvik.system.NativeStart.main(Native Method)
05-22 09:37:01.394: E/AndroidRuntime(13370): Caused by: java.lang.NullPointerException
05-22 09:37:01.394: E/AndroidRuntime(13370): at com.Project.MainActivity.MainActivity.onCreate(MainActivity.java:48)
05-22 09:37:01.394: E/AndroidRuntime(13370): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-22 09:37:01.394: E/AndroidRuntime(13370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1666)
05-22 09:37:01.394: E/AndroidRuntime(13370): ... 11 more

res/layouts/main.xml ...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ProgressBar
android:id="@+id/progressBar1"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:maxHeight="10dip"
android:minHeight="10dip" />
</LinearLayout>

this.appView 为 null,但它不应该是按照 super.onCreate(...) 调用显示的 here :

 313     /**
314 * Create and initialize web container with default web view objects.
315 */
316 public void init() {
317 CordovaWebView webView = new CordovaWebView(CordovaActivity.this);
318 CordovaWebViewClient webViewClient;
319 if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
320 {
321 webViewClient = new CordovaWebViewClient(this, webView);
322 }
323 else
324 {
325 webViewClient = new IceCreamCordovaWebViewClient(this, webView);
326 }
327 this.init(webView, webViewClient, new CordovaChromeClient(this, webView));
328 }
329
330 /**
331 * Initialize web container with web view objects.
332 *
333 * @param webView
334 * @param webViewClient
335 * @param webChromeClient
336 */
337 @SuppressLint("NewApi")
338 public void init(CordovaWebView webView, CordovaWebViewClient webViewClient, CordovaChromeClient webChromeClient) {
339 LOG.d(TAG, "DroidGap.init()");
340
341 // Set up web container
342 this.appView = webView;
343 this.appView.setId(100);
344
345 this.appView.setWebViewClient(webViewClient);
346 this.appView.setWebChromeClient(webChromeClient);

最佳答案

嗯,NullPointerException 非常简单:一个变量为 null,而您正在尝试访问它的字段/方法。

如果第 48 行是

this.appView.setWebChromeClient(new WebChromeClient() {

appView 很有可能为 null。

我不了解 Cordova,但您可能应该检查 DroidGap 生命周期并查看 Javadoc 中提供的示例.

似乎你需要在某个时候调用 super.init() 来初始化 appView。

关于来自 DroidGap > onCreate > setWebChromeClient 和 ProgressBar 实现的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16697458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com