0

自从我上一次 Java 课程以来已经有好几年了,更不用说在工作中从未写过 JAVA 了。

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

我已经接受了这篇文章的建议,但我没有做正确的事情......每个DroidGap.java源都有 appView,所以它不能为空。事实上,我不知道这里指向 null 的是什么。也许我把这段代码放在了错误的地方?

希望我有更多时间在这里深入研究......如果有人有一些建议,我会全神贯注,好吧,无论如何都要全神贯注。

有这个pastebin 帖子看起来与上面的帖子相同,所以我考虑在 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

资源/布局/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 为空,但不应按照 super.onCreate(...) 调用,如此处所示

 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);
4

1 回答 1

4

好吧,NullPointerException 非常简单:一个变量为 null,而您正试图访问其上的字段/方法。

如果第 48 行是

this.appView.setWebChromeClient(new WebChromeClient() {

appView 很有可能为空。

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

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

于 2013-05-22T17:24:32.073 回答