【问题标题】:Android Dialog NoSuchMethodException error when using XML onClick使用 XML onClick 时出现 Android Dialog NoSuchMethodException 错误
【发布时间】:2012-09-16 22:55:52
【问题描述】:

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

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

我这样显示来自 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>

在Activity上实现了按钮方法Action_ShowDialog_Buy:

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 上。

我想我理解这是某种范围问题,但我无法理解它。请注意,我已经阅读了Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs,但我需要了解,而不仅仅是复制代码。

非常感谢

【问题讨论】:

  • 您的活动有两种相同的方法?公共无效 Action_ShowDialog_Buy(查看视图)。一个显示,第二个隐藏?
  • @Pasha 不,只显示一个。我想使用 Dialog.dismiss 进行隐藏。
  • 但是你这样写:按钮方法Action_ShowDialog_Buy是在Activity上实现的: public void Action_ShowDialog_Buy(View view) { BuyDialog.dismiss() ; } 和这个 //BuyActivity.java public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shop);初始化_PR();显示_PR(); BuyDialog=新对话框(这个); BuyDialog.setContentView(R.layout.dialog_buy); } public void Action_ShowDialog_Buy(View view) { BuyDialog.show() ; }
  • 我不明白hide方法在哪里
  • 没有隐藏方法。我只是调用 dialog.dismiss 来隐藏它。

标签: java android dialog onclick nosuchmethod


【解决方案1】:

Dialog uses 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'

要摆脱这种情况,只需使用适当的充气机

LayoutInflater.from(context) 改为

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

  2. getLayoutInflater()

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

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

最后请不要忘记在 Activity 中定义 android:onClick="method" 而不是在您的自定义类中

【讨论】:

    【解决方案2】:

    你必须在你的 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>
    

    【讨论】:

    • +1,因为我不知道为什么有人不赞成这个。 (评论,有人吗?)
    【解决方案3】:

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

    我已经设法通过创建一个派生自 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() ;
    }
    

    我通过阅读Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs 设法做到了这一点,但我仍然不明白这个上下文问题。

    【讨论】:

      【解决方案4】:

      及以下:

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

      锁定这个ActionShowDialog_Buy,你忘记了方法名称中的符号_

      【讨论】:

      • 抱歉,这个是复制到这里的错误。在实际代号上是可以的。上面编辑修复。
      【解决方案5】:

      您正在尝试调用方法“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();
          }
      
      });
      

      【讨论】:

      • 谢谢,我会尝试...但是为什么错误看起来好像在 Activity 中找不到方法? java.lang.NoSuchMethodException:BuyActivity.ActionShowDialog_Buy
      • 我没有答案:P
      • @Envite 阅读完整错误,它还说:in the activity class android.view.ContextThemeWrapper。
      • 感谢@SimonAndréForsberg。我不完全理解。
      • @Perroloco: 无法编译:Eclipse 向我展示:View 类型中的方法 setOnClickListener 不适用(View.OnClickListener)不适用于参数(新的 onClickListener(){})
      猜你喜欢
      • 2011-05-13
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多