jeevesh kumar
jeevesh kumar

Reputation: 69

App is closing Unexpectedly in Android

I am unable to figure out the thing that why my simple webview app is getting closed unexpectedly. Even if the Eclipse is not throwing any error.

Here is the screenshot http://pbrd.co/YNWFVw

MainActivity.java:

package com.example.testwebview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        WebView webView1 = (WebView) findViewById(R.id.webView1);
        webView1.loadUrl("http://www.google.com/m");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_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=".MainActivity" >

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

</RelativeLayout>

testwebview Menifest :

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testwebview.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>

    </application>

</manifest>

Upvotes: 0

Views: 687

Answers (2)

Jschools
Jschools

Reputation: 2768

Always call through to the super's lifecycle callbacks before you do anything else, i.e. you should call super.onCreate(savedInstanceState) before setContentView(...).

What's actually causing the crash is most likely a NullPointerException. Calling setContentView(...) is what actually creates your views. If you call findViewById(...) before that, it will return null because your layout hasn't been inflated yet. Use LogCat in Eclipse's DDMS to get the stack trace, and you'll find that you're calling .loadUrl(...) on null.

Here's what your onCreate method should look like:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    WebView webView1 = (WebView) findViewById(R.id.webView1);
    webView1.loadUrl("http://www.google.com/m");
}

Another note: don't use fill_parent for your heights and widths -- it is deprecated. Use match_parent instead.

Upvotes: 0

Pragnani
Pragnani

Reputation: 20155

Get the web view after setting the content view i.e

WebView webView1 = (WebView) findViewById(R.id.webView1); // this returns null because you haven't set contentView, so when you try to access it, it will throw NullPointerException

So get the webView reference after setContentView like this

setContentView(R.layout.activity_main);
  WebView webView1 = (WebView) findViewById(R.id.webView1);

Edit:

For your second query, webview opening url in default browser, use this code

Webview by default uses browser to open the url, we need to do something like this

webView1.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl("http://www.google.com/m");
          return true;
           }}); 

Upvotes: 5

Related Questions