WebView在另一个布局中不会加载,但在Main中工作正常

发布于 2025-01-17 19:13:11 字数 4088 浏览 2 评论 0原文

我正在研究一个Android Project,该项目重新设置了一个主屏幕,该屏幕暂时只有图像按钮,但是想访问不同的布局,它可以访问其他布局,但是在为WebView使用相同的代码时,WebView时拒绝加载并仅显示空白屏幕,相同的代码在主要活动上效果很好,但是现在给我麻烦,我不确定从这里去哪里,我只在第二个活动上有一个图像作为理智检查我的按钮要去正确的位置

主要活动

package com.example.bsc_group;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        ImageButton foodButton = (ImageButton) findViewById(R.id.imageButtonFood);
        foodButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            }
        });

        ImageButton techButton = (ImageButton) findViewById(R.id.imageButtonTech);
        techButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            }
        });

        ImageButton calendarButton = (ImageButton) findViewById(R.id.imageButtonCalendar);
        calendarButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setContentView(R.layout.bcs_calendar_widget);
            }
        });
    }
}

第二活动

package com.example.bsc_group;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class BcsCalendarWidget extends AppCompatActivity {
    public WebView webView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        webView = findViewById(R.id.webviewWidget);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://google.com");
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        setContentView(R.layout.bcs_calendar_widget);
        {
            //Spinner spinnerBcsListings = findViewById(R.id.spinner_bcs_group);
            //ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.bcs_group_listings, android.R.layout.simple_spinner_item);
            //adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
            //spinnerBcsListings.setAdapter(adapter);
        }
    }
    @Override
    public void onBackPressed() {
        if (webView.canGoBack()){
            webView.goBack();
        }else {
            setContentView(R.layout.activity_main);}
    }
}

XML文件以进行第二个活动

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <WebView
        android:id="@+id/webviewWidget"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher_foreground" />
</RelativeLayout>

I am working on an android project that reqires a home screen that I just have image buttons on for the time being, but would like to access different layouts, it access the different layouts just fine, but when using the same code for webview, webview refuses to load and just displays a blank screen, the same code worked fine on the main activity, but is giving me trouble now and im not sure where to go from here, I only have an image on the second activity as a sanity check that my buttons were going to the right places

Main Activity

package com.example.bsc_group;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        ImageButton foodButton = (ImageButton) findViewById(R.id.imageButtonFood);
        foodButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            }
        });

        ImageButton techButton = (ImageButton) findViewById(R.id.imageButtonTech);
        techButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            }
        });

        ImageButton calendarButton = (ImageButton) findViewById(R.id.imageButtonCalendar);
        calendarButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setContentView(R.layout.bcs_calendar_widget);
            }
        });
    }
}

Second activity

package com.example.bsc_group;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class BcsCalendarWidget extends AppCompatActivity {
    public WebView webView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        webView = findViewById(R.id.webviewWidget);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://google.com");
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        setContentView(R.layout.bcs_calendar_widget);
        {
            //Spinner spinnerBcsListings = findViewById(R.id.spinner_bcs_group);
            //ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.bcs_group_listings, android.R.layout.simple_spinner_item);
            //adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
            //spinnerBcsListings.setAdapter(adapter);
        }
    }
    @Override
    public void onBackPressed() {
        if (webView.canGoBack()){
            webView.goBack();
        }else {
            setContentView(R.layout.activity_main);}
    }
}

XML file for second activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <WebView
        android:id="@+id/webviewWidget"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher_foreground" />
</RelativeLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦妄 2025-01-24 19:13:12

问题是您没有开放活动。仅给出上下文视图而无需执行代码。这个坏。每当您想转到另一个活动时,请使用以下代码:

Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);

也在onbackpressed中,我发现您只是在做setContentView。不要那样做。而是使用此:

finish();

The issue is that you are not opening the activity. Only giving a context view without executing the code. This bad. Whenever you want to go to another activity, use this code:

Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);

Also in the onBackpressed I see that you are just doing setContentView. Don't do that. Use this instead:

finish();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文