I recently started learning android, working in the book 'Android Programming for Beginners'.
In chapter 4 I have written the following code:
mainActivity.java:
package com.gamecodeschool.exploringlayouts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
}
void loadConstraintLayout(View v){
setContentView(R.layout.activity_main);
}
void loadTableLayout(View v){
//setContentView(R.layout.my_table_layout);
}
void loadMenuLayout(View v){
setContentView(R.layout.main_menu);
}
}
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Menu"
android:textSize="50sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="100sp"
android:ems="10"
android:inputType="textMultiLine"
android:padding="15sp"
android:text="Select a layout type to view an example. The onClick attribute of each button will call a method which executes setContentView to load the new layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".7"
android:text="Load ConstraintLayout"
android:textSize="20sp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:onClick="loadConstraintLayout"
android:text="Load" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".7"
android:text="Load TableLayout"
android:textSize="20sp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:onClick="loadTableLayout"
android:text="Load" />
</LinearLayout>
<RatingBar
android:id="@+id/ratingBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15sp"
android:layout_marginTop="75sp"
android:layout_marginRight="15sp" />
</LinearLayout>
When you run the program and click either of the buttons, the program crashes and returns the following error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gamecodeschool.exploringlayouts, PID: 10078
java.lang.IllegalStateException: Could not find method loadConstraintLayout(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'button3'
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:436)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I thought it may have been something I had done so I downloaded the code directly from the publisher and it returns the same error.