创建Android应用程序,但不断收到停止错误

时间:2016-05-28 20:22:27

标签: android android-studio

我有一个我创建的Android应用程序,它不断收到“App Has Stopped”错误,无法在任何地方找到帮助。有人可以帮忙。

我的代码在下面

主要活动

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private WebView webView = (WebView)findViewById(R.id.webView);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("http://www.mybrockvillenews.com/");
        webView.setWebViewClient(new WebViewClient());

    }

    @Override
    public void onBackPressed() {
        if(webView.canGoBack()){
            webView.goBack();
        } else {
        super.onBackPressed();
    }
}}

活动主要.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mylondonnews.knowbrockvilleuser.mybrockvillenews.MainActivity">

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webView2"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true" />
</RelativeLayout>

Android Manifest

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

我该如何解决这个问题?已经一次又一次地创建新的应用程序,并得到相同的错误。

我的Logcat位于

之下
05-28 16:30:40.771 32193-32193/com.mylondonnews.knowbrockvilleuser.mybrockvillenews I/InjectionManager: Inside getClassLibPath + mLibMap{0=, 1=}
05-28 16:30:40.781 32193-32193/com.mylondonnews.knowbrockvilleuser.mybrockvillenews I/InjectionManager: Inside getClassLibPath caller 
05-28 16:30:41.161 32193-32193/com.mylondonnews.knowbrockvilleuser.mybrockvillenews D/InjectionManager: InjectionManager
05-28 16:30:41.161 32193-32193/com.mylondonnews.knowbrockvilleuser.mybrockvillenews D/InjectionManager: fillFeatureStoreMap com.mylondonnews.knowbrockvilleuser.mybrockvillenews
05-28 16:30:41.161 32193-32193/com.mylondonnews.knowbrockvilleuser.mybrockvillenews I/InjectionManager: Constructor com.mylondonnews.knowbrockvilleuser.mybrockvillenews, Feature store :{}
05-28 16:30:41.161 32193-32193/com.mylondonnews.knowbrockvilleuser.mybrockvillenews I/InjectionManager: featureStore :{}
05-28 16:30:41.301 32193-32193/com.mylondonnews.knowbrockvilleuser.mybrockvillenews D/AndroidRuntime: Shutting down VM
05-28 16:30:41.301 32193-32193/com.mylondonnews.knowbrockvilleuser.mybrockvillenews E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mylondonnews.knowbrockvilleuser.mybrockvillenews, PID: 32193
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mylondonnews.knowbrockvilleuser.mybrockvillenews/com.mylondonnews.knowbrockvilleuser.mybrockvillenews.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2968)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233)
at android.app.ActivityThread.access$1000(ActivityThread.java:197)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6873)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:68)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:190)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:172)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:512)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184)
at com.mylondonnews.knowbrockvilleuser.mybrockvillenews.MainActivity.<init>(MainActivity.java:10)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1690)
at android.app.Instrumentation.newActivity(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2958)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233) 
at android.app.ActivityThread.access$1000(ActivityThread.java:197) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6873) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

2 个答案:

答案 0 :(得分:1)

不得在课程级别初始化View。这条线 private WebView webView = (WebView)findViewById(R.id.webView);必须像:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initialize WebView after setContentView()
        webView = (WebView)findViewById(R.id.webView);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("http://www.mybrockvillenews.com/");
        webView.setWebViewClient(new WebViewClient());

    }

    @Override
    public void onBackPressed() {
        if(webView.canGoBack()){
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

您正在使用WebView并尝试在其中显示网站。所以,简单来说,你就是缺少权限。添加INTERNET权限。

<uses-permission android:name="android.permission.INTERNET"/>

答案 1 :(得分:0)

尝试更改代码:

public class MainActivity extends AppCompatActivity {
private WebView webView = (WebView)findViewById(R.id.webView);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.loadUrl("http://www.mybrockvillenews.com/");
    webView.setWebViewClient(new WebViewClient());

}

为:

public class MainActivity extends AppCompatActivity {
private WebView webView; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView = (WebView)findViewById(R.id.webView);

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.loadUrl("http://www.mybrockvillenews.com/");
    webView.setWebViewClient(new WebViewClient());

}   

如果仍有强制关闭,请使用logcat日志或dumpstate日志进行检查,日志应该有致命的异常信息。