1

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>
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Mar 31 '22 at 09:54

1 Answers1

0

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();
Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38