Android应用程序中的java.lang.NullPointerException

时间:2013-10-08 14:15:01

标签: java android webview nullpointerexception

我对java很新,我一直在努力学习,所以我为我的网站制作了一个webview应用程序,我已经发布了一些人报告了一个错误,如下所示:

java.lang.NullPointerException
at com.alexriggs.android.mafiastreetwars.MafiaStreetWarsActivity$MyWebViewClient.shouldOverrideUrlLoading(MafiaStreetWarsActivity.java:87)
at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:227)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:334)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3704)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)

不知道我知道java.lang.NullPointerException意味着变量为空但被用作非空。在回顾我的代码之后,它永远不会是空的,我无法重现它。所以这是我的代码,谁能弄明白什么是错的?请记住,我是Java的新手,所以请尽量使用详细信息!

package com.alexriggs.android.mafiastreetwars;

import com.alexriggs.android.mafiastreetwars.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MafiaStreetWarsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.exit:{
                Toast.makeText(this, "Thanks For playing Mafia Street Wars!", Toast.LENGTH_LONG).show();
                finish();
                break;
            }
            case R.id.select_and_copy:
            {
                Toast.makeText(getApplicationContext(), "Select Text by dragging over the text you want to copy.", Toast.LENGTH_LONG).show();
                selectAndCopyText(myWebView);
                return true;
            }
        }
        return true;
    }
    private void selectAndCopyText(WebView view) {
        try
        {
            KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                                                    KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
            shiftPressEvent.dispatch(view);
        }
        catch (Exception e)
        {
            Log.e("dd", "Exception in emulateShiftHeld()", e);
        }

    }
    final Activity activity = this;
    WebView myWebView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Remember...");
        alertDialog.setMessage("This app is just for mobile access. This game is meant to be played on the computer at MafiaStreetWars.com");
        alertDialog.setButton("Thanks for the reminder!", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
              // here you can add functions
           }
        });
        alertDialog.setIcon(R.drawable.icon);
        alertDialog.show();
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.getSettings().setLoadWithOverviewMode(true);
        myWebView.getSettings().setUseWideViewPort(true);
        myWebView.getSettings().setPluginsEnabled(true);
        Toast.makeText(getApplicationContext(), "Page is loading... Please wait!", Toast.LENGTH_LONG).show();
        myWebView.loadUrl("http://mafiastreetwars.com/?pk_campaign=Android&pk_kwd=MainApp");
    }
    private class MyWebViewClient extends WebViewClient {


        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("mafiastreetwars.com")) {
                return false;
            }  
            // Otherwise, give the default behavior (open in browser)
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
        }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {  
            myWebView.goBack();  
            return true;  
        }  
        return super.onKeyDown(keyCode, event);  
    }  
}

3 个答案:

答案 0 :(得分:1)

这可能是由于Uri.parse(url).getHost()返回null。在致电equals()之前检查一下。

答案 1 :(得分:1)

有问题的行if (Uri.parse(url).getHost().equals("mafiastreetwars.com")) {

看起来您的网址为空,或者解析结果为空但不太可能。在使用之前检查一下是否为空!

答案 2 :(得分:1)

我建议你多做一点nullsave:

if (url==null)
   return false;
Uri lUri = Uri.parse(url);
if ("mafiastreetwars.com".equals(lUri.getHost())) {
            return false;
} 

解决了您的技术问题,但也许不是您的商业案例。