0

通过 webview 调用网页时,onPageFinished() 会抛出空指针异常。该问题仅在 Android 2.3 上仍然存在,在 Android 4.1 上运行良好。这是我的代码:

    public class StoreLocatorActivity extends Activity {

    // Declare WebView
    WebView storeLocator;
    final StoreLocatorActivity activity = this;

    boolean homeUrl = false;

    // Declare MenuItems
    MenuItem homeItem;
    MenuItem locationsItem;
    MenuItem preQualifyItem;
    MenuItem productsItem;
    MenuItem logOutItem;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
                Window.PROGRESS_VISIBILITY_ON);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_store_locator);

        setTitle("");


        // Changes to ActionBar
        /*ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.LTGRAY));
        bar.setDisplayShowHomeEnabled(false);// This prevent icon from being
                                                    // displayed on ActionBar
*/    
        callHome();
        //logOutItem.setVisible(false).setEnabled(false);
    }

    public void callHome() {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setCancelable(false);

        storeLocator = (WebView) findViewById(R.id.storeLocator);
        storeLocator.setWebViewClient(new WebViewClient());
        //storeLocator.getSettings().setJavaScriptEnabled(true);
        // storeLocator.getSettings().setBuiltInZoomControls(true);
        storeLocator.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                progressDialog.show();
                progressDialog.setProgress(0);
                activity.setProgress(progress * 1000);

                progressDialog.incrementProgressBy(progress);

                if (progress == 100 && progressDialog.isShowing())
                    progressDialog.dismiss();
            }
        });

        storeLocator.loadUrl(getResources().getString(R.string.home));
        //storeLocator.loadUrl("http://192.168.0.24/EMG/Login.aspx");

        storeLocator.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {

                if (url.equals(getResources().getString(R.string.accounts))||url.equals(getResources().getString(R.string.accountsalt))) {
                    homeUrl = true;
                    System.out.println("in here!!!!!!");
                    logOutItem.setVisible(true).setEnabled(true);
                    preQualifyItem.setVisible(false).setEnabled(false);

                } else
                    logOutItem.setVisible(false).setEnabled(false);
                System.out.println("Changed url is " + url);
                super.onPageFinished(view, url);

            }
        });

    }

菜单项实例化:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_store_locator, menu);
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    homeItem = menu.findItem(R.id.home);
    locationsItem = menu.findItem(R.id.location);
    preQualifyItem = menu.findItem(R.id.preQualify);
    productsItem = menu.findItem(R.id.products);
    logOutItem = menu.findItem(R.id.logOut);
    // logOutItem = menu.findItem(R.id.logOut);

    return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.home:

        locationsItem.setIcon(R.drawable.locations72);
        preQualifyItem.setIcon(R.drawable.prequalify72);
        productsItem.setIcon(R.drawable.products72);
        // logOutItem.setVisible(true);

        item.setIcon(R.drawable.homeb);

        final ProgressDialog homeDialog = new ProgressDialog(this);
        homeDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        homeDialog.setCancelable(false);

        storeLocator = (WebView) findViewById(R.id.storeLocator);
        storeLocator.setWebViewClient(new WebViewClient());
        //storeLocator.getSettings().setJavaScriptEnabled(true);
        // storeLocator.getSettings().setBuiltInZoomControls(true);
        storeLocator.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                homeDialog.show();
                homeDialog.setProgress(0);
                activity.setProgress(progress * 1000);

                homeDialog.incrementProgressBy(progress);

                if (progress == 100 && homeDialog.isShowing())
                    homeDialog.dismiss();
            }
        });
...........................................

这是例外:

05-02 14:06:42.632: E/AndroidRuntime(320): Uncaught handler: thread main exiting due to uncaught exception
05-02 14:06:42.672: E/AndroidRuntime(320): java.lang.NullPointerException
05-02 14:06:42.672: E/AndroidRuntime(320):  at com.emg.emgstorelocator.StoreLocatorActivity$2.onPageFinished(StoreLocatorActivity.java:117)
05-02 14:06:42.672: E/AndroidRuntime(320):  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:242)
05-02 14:06:42.672: E/AndroidRuntime(320):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 14:06:42.672: E/AndroidRuntime(320):  at android.os.Looper.loop(Looper.java:123)
05-02 14:06:42.672: E/AndroidRuntime(320):  at android.app.ActivityThread.main(ActivityThread.java:4363)
05-02 14:06:42.672: E/AndroidRuntime(320):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 14:06:42.672: E/AndroidRuntime(320):  at java.lang.reflect.Method.invoke(Method.java:521)
05-02 14:06:42.672: E/AndroidRuntime(320):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 14:06:42.672: E/AndroidRuntime(320):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 14:06:42.672: E/AndroidRuntime(320):  at dalvik.system.NativeStart.main(Native Method)

无法找出异常的原因。

4

0 回答 0