I am programming an application in Java that consists of a menu with multiple selections each bringing you to a view with a webview and a back button. My webviews are currently not displaying the URL I have put in. However I am not recieving any errors in my logcat and my program seems to be running correctly other than the webviews just staying blank. Here is my code:
package com.fox.hipnyc;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.loadUrl("https://www.google.com/");
}
});}}
And here is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/button1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Back" />
</RelativeLayout>
您是否在清单<uses-permission android:name="android.permission.INTERNET" />
设置了互联网许可
You are not seeing anything because you are only calling load when you click the back Button
. You have to call it from outside your Button
onclick()
method. Like this:
switchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
});
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.loadUrl("https://www.google.com/");
After that change, it will work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.