简体   繁体   中英

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!

put this line in on create after setContentView

private WebView mWebView;

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

}

create object of webview before onCreate() .

private WebView mWebView;

then initialized webview object after setContentView.

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

In this line

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

Which is above the onCreate, you are trying to instantiate the web view even before the view is set using setContent view, so you'll receive null pointer exception. Please remove it. It will work.

Put this line into onCreateView() you are going wrong with findiview out of scope

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

you have to do it as following

private WebView;

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM