Mike Mike - 6 months ago 31
Java Question

my application keeps receiving "Unfortunately "My App" has stopped" after Running the app

After Implementing a WebView Client my application keep receiving "Unfortunately "My App" has stopped" after Running the app.
This is my maniefest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="amapps.com.uhss" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="UHS Sword And Shield"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="UHS Sword and Shield" >
android:hardwareAccelerated="false"

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>




As you can see I have already implemented the internet permissions

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

private WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(amapps.com.uhss.R.layout.activity_main);
WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WebViewClient());

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(false);
mWebView.loadUrl("http://uhsswordandshield.com/");
mWebView.getSettings().setSupportMultipleWindows(true);

}


So could someone please tell me what is wrong with my manifest or main activity.
My app was running before I implemented a WebView client into my app to handle opening links within my app. Thanks!

Answer

Remove line

private WebView mWebView = (WebView) findViewById(R.id.activity_main_webview); 

after

 public class MainActivity extends Activity

Whenever you want to initialize something, make sure you initialize them after setContentView . Your problem is that you initialized the webView twice, the first one you initialized after public class MainActivity extends Activity hence you will get nullpointerException error.