WebView в моем приложении Android не открывает запрошенный URL -адресAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 WebView в моем приложении Android не открывает запрошенный URL -адрес

Сообщение Anonymous »

Я создал приложение, которое загружает сайт: adcpc.funwithpasswords.com и имеет код, который идет так:

Код: Выделить всё

package com.flamenzaapps.adbits;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

WebView mainWebView;
LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize main WebView
mainWebView = findViewById(R.id.webview);
layout = findViewById(R.id.layout);

// Enable JavaScript for main WebView
WebSettings mainWebSettings = mainWebView.getSettings();
mainWebSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new WebViewClient());
mainWebView.loadUrl("https://www.adcpc.funwithpasswords.com");

// Set up a WebViewClient with URL loading handler
mainWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// When a link is clicked, open it in a new smaller WebView
openNewWebView(url);
return true;  // Prevent the default browser to open the link
}
});
}

// Function to create and display a new smaller WebView on the same page
private void openNewWebView(String url) {
// Log the URL for debugging
Log.d("WebView", "Opening URL: " + url);

// Create a new WebView
WebView newWebView = new WebView(this);

// Set up WebSettings for the new WebView
WebSettings webSettings = newWebView.getSettings();
webSettings.setJavaScriptEnabled(true);  // Enable JavaScript
newWebView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e("WebView", "Error loading URL: " + description);
}
});

// Load the clicked URL in the new WebView
newWebView.loadUrl(url);

// Set layout parameters for the new WebView (make it smaller and position it)
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
600 // Fixed height for the smaller WebView
);
params.topMargin = 20;  // Optional: Add margin or positioning logic if needed
newWebView.setLayoutParams(params);

// Optionally set a background color for debugging visibility
newWebView.setBackgroundColor(Color.parseColor("#000000"));

// Ensure the parent layout is properly initialized
LinearLayout layout = findViewById(R.id.layout);  // Make sure this ID is correct
if (layout != null) {
// Add the new WebView to the layout on the main UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
layout.addView(newWebView);
Log.d("WebView", "Loaded URL: " + url);
}
});
} else {
Log.e("WebView", "Layout not found. Make sure layout is initialized.");
}

}
}
В своем файле mainactivity.java . Я хочу сделать здесь:
Есть Go to Google[/code]
всякий раз, когда я нажимаю на эту ссылку, это создает новый WebView и загружать сайт Google.com там. Но такого не происходит. Только logcat показывает сообщение:
Открытие URL: https://www.google.com/
Загруженный URL: https://www.google.com/
Но на самом деле ничего не загружено. Как решить проблему? Продвинутая спасибо.

Подробнее здесь: https://stackoverflow.com/questions/795 ... uested-url
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»