user3495677
user3495677

Reputation: 5

Simple webView keeps crashing app

I'm lost but trying in the android world. App runs fine at first, both in emulator and on actual device. But as soon as I go to the activity with a webview, the app instantly crashes, both on device, and emulator. Can anyone spot the problem? (Is it even in my code?)

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>

Manifest:

<?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 showed this:

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

If there is anything else I show that can help you to help me let me know. TIA

edit:

and

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" />

Upvotes: 0

Views: 995

Answers (2)

singularhum
singularhum

Reputation: 5122

As cybersam mentioned your webView is null. Since the WebView is actually in the fragment, you need to load it within the fragment, not in the onCreate of AutosActivity.

Therefore, it must be done in your PlaceholderFragment class instead.

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;
    }
}

Upvotes: 1

cybersam
cybersam

Reputation: 67044

Your onCreate() is loading the activity_autos layout, but the layout with the webView1 ID is actually fragment_autos. Therefore, webView will be null, and webView.loadUrl() will throw the NullPointerException in the stack trace.

Upvotes: 0

Related Questions