1

我正在开发一个带有内部 web 视图的应用程序,仅限于一个站点,我希望它有一个向后刷新停止等...

这是我的代码:

package com.csu.stan.asiusu;



import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class Site extends SherlockActivity {
private WebView myWebView;
private MenuItem fwdmenu, bckmenu, refmenu;
private boolean refresh = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_site);
    //webview
    myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new mywebclient());
    myWebView.setBackgroundColor(0);
    myWebView.setBackgroundResource(R.drawable.back);
    myWebView.clearHistory();
    myWebView.clearCache(true);
    myWebView.loadUrl("http://www.csustan.edu/asi-usu/new_index.php");
    //webview settings
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
    webSettings.setBuiltInZoomControls(true);

    // Show the Up button in the action bar.
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

//code called to update all icons
    public void updateicons() {
        if(myWebView.canGoBack()) {
            bckmenu.setIcon(R.drawable.ic_menu_back);
            bckmenu.setEnabled(true);}
        else {
            bckmenu.setEnabled(false);
            bckmenu.setIcon(R.drawable.ic_menu_back_d); }
        if(myWebView.canGoForward()) {
            fwdmenu.setIcon(R.drawable.ic_menu_forward);
            fwdmenu.setEnabled(true);}
        else {
            fwdmenu.setEnabled(false);
            fwdmenu.setIcon(R.drawable.ic_menu_forward_d);}
    }

private class mywebclient extends WebViewClient {
    // handles "mailto:" html and opens the action chooser
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("mailto:")) { 
                Intent intent = new Intent(Intent.ACTION_SENDTO,
                        Uri.parse(url)); 
                startActivity(intent);
                return true;
        }else if(url.startsWith("http:") || url.startsWith("https:")) {
            myWebView.loadUrl(url);
            return false;
        }
        return true;
    }
    //on page load 
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        //change refresh to stop
        refresh = false;
        refmenu.setIcon(R.drawable.ic_menu_stop);
        updateicons();
    }
    //on page finished
    public void onPageFinished(WebView view, String url) {
        //change stop to refresh
        updateicons();
        refmenu.setIcon(R.drawable.ic_menu_refresh);
        refresh = true;
    }
}

@Override
public void onBackPressed()
{
    // choose what back button does 
    if(myWebView.canGoBack())
        myWebView.goBack();
    else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.activity_browsermenu, menu);
    fwdmenu = menu.findItem(R.id.fwd);
    bckmenu = menu.findItem(R.id.bck);
    refmenu = menu.findItem(R.id.ref);
    inflater.inflate(R.menu.activity_open, menu);
    updateicons();
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    //refresh/stop button
    case R.id.ref:
        if(refresh == true) 
            myWebView.reload();
        if(refresh == false)
            myWebView.stopLoading();
        return true;
    //back button
    case R.id.bck:
        if(myWebView.canGoBack())
            myWebView.goBack();
        updateicons();
        return true;
    //forward button
    case R.id.fwd:
        if(myWebView.canGoForward()) 
            myWebView.goForward();
        updateicons();
        return true;
    //open in browser button
    case R.id.open:
        Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.csustan.edu/asi-usu/new_index.php"));
        startActivity(intent);
        return true;
    //home button
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

这在纵向上工作得很好,但由于某些奇怪的原因它在横向上崩溃,这是错误:

如果我删除 onpagestarted 中的所有代码,则工作正常,但这显然会破坏刷新/停止。

05-06 11:09:05.594: E/AndroidRuntime(21915): FATAL EXCEPTION: main
05-06 11:09:05.594: E/AndroidRuntime(21915): java.lang.NullPointerException
05-06 11:09:05.594: E/AndroidRuntime(21915):    at com.csu.stan.asiusu.Site.updateicons(Site.java:53)
05-06 11:09:05.594: E/AndroidRuntime(21915):    at com.csu.stan.asiusu.Site$mywebclient.onPageStarted(Site.java:85)
05-06 11:09:05.594: E/AndroidRuntime(21915):    at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:314)
05-06 11:09:05.594: E/AndroidRuntime(21915):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 11:09:05.594: E/AndroidRuntime(21915):    at android.os.Looper.loop(Looper.java:137)
05-06 11:09:05.594: E/AndroidRuntime(21915):    at android.app.ActivityThread.main(ActivityThread.java:5226)
05-06 11:09:05.594: E/AndroidRuntime(21915):    at java.lang.reflect.Method.invokeNative(Native Method)
05-06 11:09:05.594: E/AndroidRuntime(21915):    at java.lang.reflect.Method.invoke(Method.java:511)
05-06 11:09:05.594: E/AndroidRuntime(21915):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-06 11:09:05.594: E/AndroidRuntime(21915):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-06 11:09:05.594: E/AndroidRuntime(21915):    at dalvik.system.NativeStart.main(Native Method)

任何帮助将不胜感激谢谢。

4

1 回答 1

0

我认为当您旋转时,对您的 WebView 的引用丢失了。您可以再次设置它或调整清单中此活动的 android:configChanges="orientation"。

于 2013-05-06T18:30:02.217 回答