2

I just started a new appliction using android studio .. but i need to open a web page in my application i tried using web view but it doesnt worked out... when i open my app it crashes down

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

and i included in java class file

private  WebView wb;


@Override
protected void onCreate(Bundle savedInstanceState) {

 wb=(WebView)findViewById(R.id.web_view);
    WebSettings webSettings=wb.getSettings();
    webSettings.setJavaScriptEnabled(true);
    wb.loadUrl("https://www.google.co.in");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_references);
}

In manifest xml file i included

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

but still my app crashes pllzz help me

android #android-studio

Community
  • 1
  • 1
karthikmunna
  • 139
  • 1
  • 2
  • 14

1 Answers1

5

Call super method and setcontentview first. Only after setContentView you can access to the functions findViewByid and all

From the documentation

setContentView(int resLayout): Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.

So if it isn't called no views will be added to your activity. Then you cant access any views at all.

Change it like this

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_references);


   wb=(WebView)findViewById(R.id.web_view);
    WebSettings webSettings=wb.getSettings();
    webSettings.setJavaScriptEnabled(true);
    wb.loadUrl("https://www.google.co.in");
}
Emil
  • 2,786
  • 20
  • 24
  • Good job! I just wanted to make a comment, please post stacktrace ;) Because I'm to lazy to read the full code. – Rene M. Oct 28 '15 at 10:53
  • @ReneM. Yeah.. Me too wanted to post this answer as comment, but it will be too long, so just decided to post it as an answer. :) – Emil Oct 28 '15 at 10:55