在android:onClick属性的父级或祖先上下文中找不到方法 [英] Could not find method in a parent or ancestor Context for android:onClick attribute

查看:319
本文介绍了在android:onClick属性的父级或祖先上下文中找不到方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受点击的图像,点击后应打开xml布局.

I have an image that accepts click and should open an xml layout when clicked.

这是包含可点击图像的xml文件:

Here is the xml file that contains the clickable image:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
    tools:context="com.abcd.myapp.myabcdapp.activities.BeginnersActivity">

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:onClick="openHelp1"
            .../>

tools:context中可以看到,我包含了BeginnersActivity.这是我在BeginnersActivity.java中拥有的东西:

As it can be seen in the tools:context I have included BeginnersActivity. Here is what I have in BeginnersActivity.java:

public class BeginnersActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beginners);
        UiHelper.hideSystemBar(this);
    }
    public void openHelp1(View view) {
        Intent i = new Intent(this, Help1Activity.class);
        startActivity(i);
    }
    ...

这是我在Help1Activity.java中拥有的东西:

And here is what I have in Help1Activity.java:

public class Help1Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help1);
    }
    ...

我还在AndroidManifest.xml中将Help1Activity添加为:

....
<application
        android:name=".MyApp"
        android:allowBackup="true"
        android:configChanges="locale|touchscreen|orientation|screenLayout|screenSize|keyboardHidden|uiMode"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        ...
        <activity android:name=".activities.Help1Activity" />
        ....

当我单击imageView1时,应用程序崩溃并且在调试模式下,我看到以下消息:

When I click on imageView1 the app crashes and in debug mode I see this message:

致命异常:主要
java.lang.IllegalStateException:在父级或祖先上下文中找不到方法openHelp1(View)的android:onClick属性,此属性是在视图类android.support.v7.widget.AppCompatImageView上定义的,ID为'imageView1'
android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find method openHelp1(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'imageView1'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)

我有什么想念的东西吗?

Is there anything I am missing or doing incorrectly?

推荐答案

在使用带有属性android:onClick="openHelp1"

您不能以这种方式从另一个活动中调用方法.

You can't call method from another activity in this way.

这篇关于在android:onClick属性的父级或祖先上下文中找不到方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆