为什么我的应用程序在android studio中一直崩溃?(null oncreate)

时间:2017-01-31 01:21:57

标签: android android-studio android-studio-2.2

这是我的基本代码

package com.example.jorgesalgado.singular20;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;





public class MainActivity extends Activity {

// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}

private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;

private LinearLayout dotsLayout;
private TextView[] dots;
private int[] layouts;
private Button btnNext;
private WebView mWebView;




@Nullable
@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,          WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




    mWebView = (WebView) findViewById(R.id.mWebView);
    mWebView.loadUrl("file:///android_asset/index.html");
    mWebView.getSettings().setJavaScriptEnabled(true);



    viewPager = (ViewPager) findViewById(R.id.view_pager);
    dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);

这是错误

01-30 17:08:39.607 12741-12741/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process:     com.example.jorgesalgado.singular20, PID: 12741
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jorgesalgado.singular20/com.example.jorgesalgado.singular20.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
               ``                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:154)
                                                   at      android.app.ActivityThread.main(ActivityThread.java:6119)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
                                                   at com.example.jorgesalgado.singular20.MainActivity.onCreate(MainActivity.java:58)
                                                   at android.app.Activity.performCreate(Activity.java:6679)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:154) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我试图做的就是能够在android studio中使用index.html使用html和javascript。你们中有人有更好的方法吗?如果是这样,请你帮帮我。

这是XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_screen1">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:id="@+id/linearLayout">

</LinearLayout>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:layout_above="@+id/textView2"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignStart="@+id/textView2"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:id="@+id/txtOutput" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Type in the topic"
    android:textColor="@android:color/white"
    android:textSize="@dimen/slide_title"
    android:textStyle="bold"
    android:id="@+id/textView2"
    android:layout_above="@+id/textView"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/desc_padding"
    android:paddingRight="@dimen/desc_padding"

    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:textSize="@dimen/slide_desc"
    android:id="@+id/textView"
    android:layout_marginBottom="80dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="27dp"
    android:id="@+id/mWebView" />

1 个答案:

答案 0 :(得分:0)

[已更新]将tools:context=".MainActivity添加到您的根布局