简单的webView不断崩溃的应用程序

时间:2014-04-03 21:52:58

标签: java android webview

我迷失了但是在Android世界中尝试过。应用程序在模拟器和实际设备上都可以正常运行。但是当我使用webview进入活动时,应用程序会立即崩溃,无论是在设备上还是在模拟器上。谁能发现问题? (它甚至在我的代码中吗?)

AutosActivity.java:

package com.cyclist.cyclist;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.webkit.WebView;



public class AutosActivity extends ActionBarActivity {
    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autos);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
        webView = (WebView) findViewById(R.id.webView1);
        webView.loadUrl("http://www.example.com");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.autos, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_autos,
                    container, false);
            return rootView;
        }
    }

}

fragment_autos.xml:

<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.cyclist.cyclist.AutosActivity$PlaceholderFragment" >


    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cyclist.cyclist"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.cyclist.cyclist.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 <activity
            android:name="com.cyclist.cyclist.AutosActivity"
            android:label="@string/title_activity_autos"
            android:parentActivityName="com.cyclist.cyclist.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.cyclist.cyclist.MainActivity" />
        </activity>
 </application>

</manifest>

LogCat显示了这个:

04-03 13:59:12.120: E/AndroidRuntime(24267): FATAL EXCEPTION: main
04-03 13:59:12.120: E/AndroidRuntime(24267): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cyclist.cyclist/com.cyclist.cyclist.AutosActivity}: java.lang.NullPointerException
04-03 13:59:12.120: E/AndroidRuntime(24267):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at android.app.ActivityThread.access$1500(ActivityThread.java:124)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at android.os.Looper.loop(Looper.java:130)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at android.app.ActivityThread.main(ActivityThread.java:3806)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at java.lang.reflect.Method.invokeNative(Native Method)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at java.lang.reflect.Method.invoke(Method.java:507)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at dalvik.system.NativeStart.main(Native Method)
04-03 13:59:12.120: E/AndroidRuntime(24267): Caused by: java.lang.NullPointerException
04-03 13:59:12.120: E/AndroidRuntime(24267):    at com.cyclist.cyclist.AutosActivity.onCreate(AutosActivity.java:29)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-03 13:59:12.120: E/AndroidRuntime(24267):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
04-03 13:59:12.120: E/AndroidRuntime(24267):    ... 11 more

如果我还有其他任何可以帮助您帮助我告诉我的信息。 TIA

编辑:

activity_auto.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.cyclist.cyclist.AutosActivity"
    tools:ignore="MergeRootFrame" />

2 个答案:

答案 0 :(得分:1)

正如cybersam所提到的,webView为空。由于WebView实际上位于片段中,因此您需要将其加载到片段中,而不是onCreate AutosActivity中。{/ p>

因此,必须在您的PlaceholderFragment课程中完成。

public static class PlaceholderFragment extends Fragment {

    WebView webView;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_autos,
                container, false);

        // load your webview here using fragment_autos layout.
        webView = (WebView) rootView.findViewById(R.id.webView1);
        webView.loadUrl("http://www.example.com");

        return rootView;
    }
}

答案 1 :(得分:0)

您的onCreate()正在加载activity_autos布局,但webView1 ID的布局实际上是fragment_autos。因此,webView将为null,webView.loadUrl()会将NullPointerException抛出到堆栈跟踪中。