java.lang.IllegalStateException:找不到方法-Android入门书籍ERROR

BigNick Montana:

我最近开始学习android,并在“初学者的Android编程”一书中工作。

在第4章中,我编写了以下代码:

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)

我认为这可能是我做过的事情,所以我直接从发布者那里下载了代码,并且返回了相同的错误。

Rediska:

方法loadConstraintLayout应该是public

public void loadConstraintLayout(View v){
    setContentView(R.layout.activity_main);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

java.lang.IllegalStateException:在Android Studio中找不到按钮的onClick方法

无法启动活动ComponentInfo {className} java.lang.IllegalStateException:找不到颜色!(Android JetPack撰写)

java.lang.IllegalStateException:找不到任何构建目录

找不到符号 [ERROR] 符号:方法 getlogger(java.lang.Class<org.first.FirstMaven.App>)

在类android.databinding.ViewDataBinding中找不到方法safeUnbox(java.lang.Boolean)

java.lang.IllegalStateException: 找不到 PropertySource 并且设置了快速失败属性,在微服务中失败

无法解决错误[java.lang.IllegalStateException:找不到WebApplicationContext:没有注册ContextLoaderListener吗?]

引起原因:java.lang.IllegalStateException:找不到必需的键[datasource.sampleapp.hibernate.dialect]

java.lang.IllegalStateException:找不到线程绑定请求,方面是异常

java.lang.IllegalStateException:找不到用于urn:security:1.1的解析器

java.lang.IllegalStateException:@MockK时找不到sun.misc.Unsafe

使用Matcher提取子字符串:java.lang.IllegalStateException:找不到匹配项

java.lang.IllegalStateException:找不到必需的标识符属性

java.lang.IllegalStateException:找不到工厂javax.faces.context.FacesContextFactory的备份

java.lang.IllegalStateException:找不到工厂javax.faces.context.FacesContextFactory的备份

java.lang.IllegalStateException:找不到ApplicationContext,首先正确配置Grails

java.lang.IllegalStateException:找不到匹配的编辑器或转换策略

java.lang.IllegalStateException:找不到函数androidx.compose.internal.restartableFunctionInstance

由于java.lang.IllegalStateException而无法构造Spring webClient:找不到合适的默认ClientHttpConnector

ElasticSearch SpringBoot+Spring 数据:java.lang.IllegalStateException:在接口上找不到合适的构造函数

奇怪的java.lang.UnsatisfiedLinkError:找不到本机方法:

JNI- java.lang.UnsatisfiedLinkError:找不到本机方法

找不到符号 - 方法计数(java.lang.string)

Android java.lang.IllegalStateException:无法执行活动的方法

Android-java.lang.IllegalStateException:无法执行活动的方法

Android上的JIBX。LogCat显示:找不到方法'Y +异常java.lang.VerifyError引用的类'X'

onRequestPermissionsResult()中的Android java.lang.IllegalStateException

Android Kotlin:java.lang.IllegalStateException

Android中的java.lang.IllegalStateException