如何在Android Studio中打开Android应用程序中的任何网站

kar*_*nna 2 android web android-activity android-applicationinfo

我只是用机器人工作室开始了新机应用..但我需要打开一个网页在我的应用我尝试使用Web视图但它不工作了......当我打开我的应用程序崩溃下来

<WebView
    android:id="@+id/web_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
Run Code Online (Sandbox Code Playgroud)

我包含在java类文件中

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);
}
Run Code Online (Sandbox Code Playgroud)

在包含的清单xml文件中

<uses-permission android:name="android.permission.INTERNET"/>
Run Code Online (Sandbox Code Playgroud)

但仍然我的应用程序崩溃pllzz帮助我

android#android-studio

Emi*_*mil 5

首先调用super方法和setcontentview.只有在setContentView之后,您才能访问findViewByid所有功能

文档中

setContentView(int resLayout):从布局资源设置活动内容.资源将膨胀,将所有顶级视图添加到活动中.

因此,如果未调用,则不会向您的活动添加任何视图.然后你根本无法访问任何视图.

像这样改变它

@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");
}
Run Code Online (Sandbox Code Playgroud)