问题
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