[Java for Android App Development] Insayne84:
Hello,

I am currently trying to have a webview that listens to a long click and loads a website once longclicked.

However, it crashes and i would love to find out why, or how to fix it :)

I do have to state that this crash Occurs on Samsung and LG hardware, but not on the Nexus 4 or the Emulator (Nexus 7)

package com.example.homehub;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Go Fullscreen (Just in case)
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

// Display the layout
setContentView(R.layout.activity_main);

// Define the Web View
webView = (WebView) findViewById(R.id.webView1);

// Setup
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
webView.setLongClickable(true);

// Handle Links internally
webView.setWebViewClient(new WebViewClient() { });

// Using Latest Chrome Client
webView.setWebChromeClient(new WebChromeClient());

// Browser Settings
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );

// Go to Default URL
webView.loadUrl("http://google.com" );

// Long Click Listener
webView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Grab URL
String webUrl = webView.getUrl();
// Show the URL (This works)
Toast.makeText(getApplicationContext(), webUrl, Toast.LENGTH_SHORT).show();
// This request crashes
webView.loadUrl("http://www.yahoo.com");
return true;
}
});
}
// Back Button behaviour (Either browser back, or exit)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView webView = (WebView) findViewById(R.id.webView1);
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack()){
webView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}

And my Logcat:

12-01 01:48:40.579: W/dalvikvm(15534): threadid=1: thread exiting with uncaught exception (group=0x418eac08)
12-01 01:48:40.584: E/AndroidRuntime(15534): FATAL EXCEPTION: main
12-01 01:48:40.584: E/AndroidRuntime(15534): Process: com.example.homehub, PID: 15534
12-01 01:48:40.584: E/AndroidRuntime(15534): java.lang.NullPointerException
12-01 01:48:40.584: E/AndroidRuntime(15534):	at com.android.org.chromium.content.browser.LongPressDetector.dispatchLongPress(LongPressDetector.java:158)
12-01 01:48:40.584: E/AndroidRuntime(15534):	at com.android.org.chromium.content.browser.LongPressDetector.access$000(LongPressDetector.java:21)
12-01 01:48:40.584: E/AndroidRuntime(15534):	at com.android.org.chromium.content.browser.LongPressDetector$LongPressHandler.handleMessage(LongPressDetector.java:58)
12-01 01:48:40.584: E/AndroidRuntime(15534):	at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 01:48:40.584: E/AndroidRuntime(15534):	at android.os.Looper.loop(Looper.java:146)
12-01 01:48:40.584: E/AndroidRuntime(15534):	at android.app.ActivityThread.main(ActivityThread.java:5653)
12-01 01:48:40.584: E/AndroidRuntime(15534):	at java.lang.reflect.Method.invokeNative(Native Method)
12-01 01:48:40.584: E/AndroidRuntime(15534):	at java.lang.reflect.Method.invoke(Method.java:515)
12-01 01:48:40.584: E/AndroidRuntime(15534):	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
12-01 01:48:40.584: E/AndroidRuntime(15534):	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
12-01 01:48:40.584: E/AndroidRuntime(15534):	at dalvik.system.NativeStart.main(Native Method)