How to Open Any Website in Android Application in Android Studio

試著忘記壹切 提交于 2020-07-09 07:53:05

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/33389037/how-to-open-any-website-in-android-application-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!