Issue
i have a app set up which loads a website into the app which works fine, when i try to add another element to the content view the app crashes without a error, How can i add more than one element to my set content view? ive tried it with frame layout and more but they all dont seem to work. Here is my code
package com.example.fiercepcs;
import android.content.Context;
import android.os.Bundle;
import com.example.fiercepcs.databinding.ActivityMainBinding;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.app.Application;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout screenLayout = new LinearLayout(this);
screenLayout.setOrientation(LinearLayout.VERTICAL);
Button button = (Button) findViewById(R.id.button);
WebView myWebView = new WebView(getApplicationContext());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.fiercepc.co.uk");
setContentView(myWebView);
setContentView(button);
}
}
my activity main xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.FiercePcs.AppBarOverlay" />
<include
android:id="@+id/include"
layout="@layout/content_main" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:translationZ="-5dp">
</WebView>
<Button
android:id="@+id/button"
android:layout_width="312dp"
android:layout_height="179dp"
android:scaleX="1"
android:scaleY="1"
android:text="@string/button"
android:visibility="visible"
app:layout_anchor="@+id/include"
app:layout_anchorGravity="center" />
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Solution
your app crashes because your are trying to set button view and webView as the root layout, don't do that, inflate or bind the root of your layout with ActivityMainBinding
do it like this :
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bindnig = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
LinearLayout screenLayout = new LinearLayout(this);
screenLayout.setOrientation(LinearLayout.VERTICAL);
Button button = (Button) findViewById(R.id.button);
WebView myWebView = new WebView(getApplicationContext());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.fiercepc.co.uk");
}
}
Answered By - Mado
Answer Checked By - Marilyn (JavaFixing Volunteer)