我对 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);
}
}