一箫一剑平生意,负尽狂名十五年。这篇文章主要讲述java.lang.IllegalStateException:找不到方法-初学者的Android编程书ERROR [duplicate]相关的知识,希望能为你提供帮助。
此问题已经在这里有了答案:java.lang.IllegalStateException: Could not find method in a parent or ancestor Context for android:onClick attribute(3个答案)java.lang.IllegalStateExeption: Could not find a method finishA(View) in the activity class(2个答案)Why can't android:onClick recognize the specfied function?(4个答案)关闭3分钟前。我最近开始学习android,在《初学者的Android编程》一书中工作。
在第四章中,我编写了以下代码:
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>
[运行程序并单击任意一个按钮时,程序崩溃并返回以下错误:
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)
【java.lang.IllegalStateException(找不到方法-初学者的Android编程书ERROR [duplicate])】我以为可能是我做过的事情,所以我直接从发布者那里下载了代码,它返回了相同的错误。
答案方法loadConstraintLayout应为public:
public void loadConstraintLayout(View v){
setContentView(R.layout.activity_main);
}
推荐阅读
- [android 8.0中的android套接字连接
- Android-如何在ActionBar中实现“赞”按钮
- 如何在Unity中使用由android studio制作的SQLite数据库()
- 要从Android设备删除图片吗()
- 当活动不在前面时,在Android中暂停CountDownTimer
- 如何在Symfony 3.3中为单个字段(文档实体)配置文件上传器
- 如何在Symfony 3中使用纯PHP编译SASS(scss)
- 如何使用MySQL Workbench将MS SQL Server数据库(从SQL Server Management Studio)迁移到MySQL
- 如何在Winforms中为单个键注册单个或多个全局热键