我是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上。
我想我明白这是某种范围问题,但我无法理解它。请注意,我已经Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs,但我需要了解,而不仅仅是复制代码。
非常感谢
答案 0 :(得分:2)
您正在尝试调用方法“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();
}
});
答案 1 :(得分:1)
及以下:
Caused by: java.lang.NoSuchMethodException:BuyActivity.ActionShowDialog_Buy
锁定此ActionShowDialog_Buy
您忘记了方法名称中的simbol _
答案 2 :(得分:1)
您必须在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>
答案 3 :(得分:0)
感谢所有试图提供帮助的人。
我已经设法通过创建一个派生自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 :(得分:0)
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'
要摆脱这个,只需使用适当的inflater
LayoutInflater.from(context)而不是
((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE))
getLayoutInflater()
避免使用void setContentView(int layoutResID)而是使用void setContentView(View view)
在Dialog构造函数中使用相同的 context ,即super(context)
最后请不要忘记在Activity中定义android:onClick =“method”而不是在自定义类中