繁体   English   中英

使用XML onClick时出现Android Dialog NoSuchMethodException错误

[英]Android Dialog NoSuchMethodException error when using XML onClick

我是Java和Android的新手,我正在开发我的第一个测试应用程序。

我已经取得了进展,但我被一个Dialog阻止了。

我从Activity中显示对话框,如下所示:

//BuyActivity.java
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop);

    initialize_PR();
    display_PR();
    BuyDialog=new Dialog(this);
    BuyDialog.setContentView(R.layout.dialog_buy);

}
public void Action_ShowDialog_Buy(View view) {
    BuyDialog.show() ;
}

单击触发Action_ShowDialog_Buy的Activity按钮时,将正确显示该对话框。 但在那之后,Dialog本身有一个按钮:

<!-- dialog_buy.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- Other stuff -->

<Button
    android:id="@+id/Button_Buy"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Some_Other_Stuff"
    android:layout_centerHorizontal="true"
    android:text="@string/button_buy"
    android:onClick="Action_ShowDialog_Buy" />

</RelativeLayout>

按钮方法Action_ShowDialog_Buy在Activity上实现:

public void Action_ShowDialog_Buy(View view) {
    BuyDialog.dismiss() ;
}

但是当我点击对话框中的按钮时,我收到错误:

java.lang.IllegalStateException: Could not find a method BuyActivity.Action_ShowDialog_Buy(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'Button_Buy'

及以下:

Caused by: java.lang.NoSuchMethodException:BuyActivity.Action_ShowDialog_Buy

但正如您在上面所看到的,该方法存在于Activity上。

我想我明白这是一个范围问题,但我无法理解它。 请注意我已经知道在布局xml中使用onClick属性会在Android对话框中导致NoSuchMethodException,但我需要了解,而不仅仅是复制代码。

非常感谢

您正在尝试调用方法“Action_ShowDialog_Buy”,但Dialog对象中不存在此方法! 如果在xml中指定此方法,则此方法不应位于Activity中。 如果要处理Activity中的单击,则应以编程方式设置onClickListener:

Button b=(Button)BuyDialog.findViewById(R.id.Button_Buy);
b.setOnClickListener(new OnClickListener(){
    @Override
    onClick(View v){
      BuyDialog.dismiss();
    }

});

及以下:

Caused by: java.lang.NoSuchMethodException:BuyActivity.ActionShowDialog_Buy

锁定此ActionShowDialog_Buy您忘记了方法名称中的simbol _

您必须在xml文件中使用set clickable true。

<!-- dialog_buy.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <!-- Other stuff -->

        <Button
            android:id="@+id/Button_Buy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Some_Other_Stuff"
            android:layout_centerHorizontal="true"
            android:text="@string/button_buy"
            android:onClick="Action_ShowDialog_Buy"
            android:clickable="true" />

        </RelativeLayout>

感谢所有试图提供帮助的人。

我已经设法通过使用以下代码创建一个派生自Dialog的类并使用它来解决这个问题:

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

public class BuyDialogClass extends Dialog
{

//Ensure this Dialog has a Context we can use
Context mContext ;

public BuyDialogClass(Context context) {
    super(context);
    mContext=context; //Store the Context as provided from caller
}

@Override
 protected void onCreate(final Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  RelativeLayout ll=(RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.dialog_buy, null);
  setContentView(ll); 
 }

}

这允许我将对话框称为:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop);

    initialize_PR();
    display_PR();
    BuyDialog=new BuyDialogClass(this);
    //The setContentView is not necessary here as we call it on the onCreate

    //We can NOT access Dialog widgets from here,
    //because the dialog has not yet been shown.

}
public void Action_ShowDialog_Buy(View view) {
    BuyDialog.show() ;

    //NOW, after showing the dialog, we can access its widgets
    jobject_SeekBar_buy= (SeekBar) BuyDialog.findViewById(R.id.SeekBar_Dialog_Buy) ;
    jobject_SeekBar_buy.setMax(PR_num_coins/currentPR_buy_price) ;
    jobject_SeekBar_buy.setOnSeekBarChangeListener(this);

}
public void Action_Buy_PR(View view) {
    BuyDialog.dismiss() ;
}

我设法通过阅读在布局xml中使用onClick属性在Android对话框中导致NoSuchMethodException,但我仍然不理解这个上下文问题。

Dialog使用ContextThemeWrapper

现在,我们得到的例外......

java.lang.IllegalStateException: Could not find a method android:onClick="method" 
in the activity class android.view.ContextThemeWrapper
for onClick handler on view class android.widget.RadioButton with id 'statusSuspend'

要摆脱这个,只需使用适当的inflater

而不是LayoutInflater.from(context)

  1. ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE))

  2. getLayoutInflater()

避免使用void setContentView(int layoutResID)而是使用void setContentView(View view)

在Dialog构造函数中使用相同的上下文 ,即super(context)

最后请不要忘记在自定义类中定义Activity中的android:onClick =“method”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM