问题描述
我刚刚使用android studio开始了一个新的应用程序..但是我需要在我的应用程序中打开一个网页,但是我尝试使用网络视图却无法解决问题……当我打开我的应用程序时,它崩溃了
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" />
并且我包含在Java类文件中
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);
}
在我包含的清单xml文件中
In manifest xml file i included
<uses-permission android:name="android.permission.INTERNET"/>
但是我的应用程序仍然崩溃,请帮助我
but still my app crashes pllzz help me
推荐答案
首先调用super方法并设置setcontentview.只有在setContentView之后,您才能访问功能findViewByid
和所有
Call super method and setcontentview first. Only after setContentView you can access to the functions findViewByid
and all
setContentView(int resLayout)
:从布局资源设置活动内容.这 资源将会膨胀,从而将所有顶级视图添加到活动中.
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.
像这样更改它
@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");
}
这篇关于如何在Android Studio中的Android应用程序中打开任何网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!