使用XML的onClick当Android的对话框NoSuchMethodException错误 [英] Android Dialog NoSuchMethodException error when using XML onClick

查看:161
本文介绍了使用XML的onClick当Android的对话框NoSuchMethodException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Java和Android,和我的工作我的第一个测试应用程序。

I'm new to Java and Android, and I'm working on my first test app.

我已经取得了进展,但我阻止了对话。

I've progressed with it, but I'm blocked with a Dialog.

我秀从活动像这样的对话:

I show the dialog from the Activity like this:

//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按钮时,可以正确所示的对话框。但在此之后,该对话本身有一个按钮:

And the dialog is correctly shown when the button of the Activity that triggers Action_ShowDialog_Buy is clicked. But after that, the Dialog itself has a button:

<!-- 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上的活动实现的:

The button method Action_ShowDialog_Buy is implemented on the Activity:

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

但是当我点击在对话框中的按钮我收到错误消息:

but when I click on the Button in the Dialog I receive the error:

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

但你可以在上面看到,该方法上存在活动。

but as you can see above, the method exists on the Activity.

我想我明白这是某种类型的范围的问题,但我不设法了解它。请注意,我readed <一个href=\"http://stackoverflow.com/questions/4243704/using-onclick-attribute-in-layout-xml-causes-a-nosuchmethodexception-in-android\">Using在的onClick布局XML属性导致Android的对话框一个NoSuchMethodException但我需要去理解,而不仅仅是复制code。

I think I understand this is some kind of scope issue, but I do not manage to understand it. Please note that I have readed Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs but I need to understand, not just to copy code.

非常感谢

推荐答案

感谢大家,试图帮助。

我已经成功通过从对话框派生的类,并使用它,用这个code到排序了这一点:

I've managed to sort this out by creating a class derived from Dialog and using it, using this code:

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); 
 }

}

这让我打电话给那个对话框,这样的:

This allowed me to call the dialog as this:

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() ;
}

我设法做到这一点通过阅读<一href=\"http://stackoverflow.com/questions/4243704/using-onclick-attribute-in-layout-xml-causes-a-nosuchmethodexception-in-android\">Using在的onClick布局XML属性导致Android的对话框一个NoSuchMethodException ,但我还是不明白这个语境问题。

I managed to do this by reading Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs but I still do not understand this Context issue.

这篇关于使用XML的onClick当Android的对话框NoSuchMethodException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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