Android Dialog NoSuchMethodException error when using XML onClick
Asked Answered
S

5

3

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

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>

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'

and below:

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

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

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.

Many thanks

Slipper answered 25/9, 2012 at 12:10 Comment(5)
You activity have 2 same methods? public void Action_ShowDialog_Buy(View view). One for show and second for hide?Erse
@Erse No, Only one to show. I want to use Dialog.dismiss for hide.Slipper
But you write this: The button method Action_ShowDialog_Buy is implemented on the Activity: public void Action_ShowDialog_Buy(View view) { BuyDialog.dismiss() ; } and 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() ; }Erse
I don't understand where is hide methodErse
There is no hide method. I just call dialog.dismiss in order to hide it.Slipper
S
0

Thanks to everyone that tried to help.

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

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.

Slipper answered 25/9, 2012 at 21:20 Comment(0)
L
2

You are trying to call the method "Action_ShowDialog_Buy", but this method doesn't exist in the Dialog object! This method should not be in the Activity, if you specify it in the xml. If you want to handle the click in the Activity, you should set the onClickListener programatically:

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

});
Litterbug answered 25/9, 2012 at 12:15 Comment(6)
Thanks, I'll try... but why then the error reads as if it can not find the method in the Activity? java.lang.NoSuchMethodException:BuyActivity.ActionShowDialog_BuySlipper
I don't have an answer to that :PLitterbug
@Slipper read the full error, it also says: in the activity class android.view.ContextThemeWrapper.Saraisaraiya
Thanks @SimonAndréForsberg . I do not fully understand that.Slipper
@Perroloco: that does not compile: Eclipse shows me: The Method setOnClickListener is not applicable(View.OnClickListener) in the type View is not applicable for the arguments (new onClickListener(){})Slipper
Then I don't understand how my code is working...take a look: pastebin.com/H2HR8EP9Litterbug
E
1

and below:

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

lock at this ActionShowDialog_Buy you forget simbol _ in name of the method

Erse answered 25/9, 2012 at 12:18 Comment(1)
Sorry, this one was an error copying here. On the actual code name is OK. Edited above to fix.Slipper
C
1

You have to use set clickable true in your xml file.

<!-- 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>
Compilation answered 7/3, 2013 at 8:51 Comment(1)
+1 because I can't figure out why anybody downvoted this. (Comments, anybody?)Twobyfour
S
0

Thanks to everyone that tried to help.

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

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.

Slipper answered 25/9, 2012 at 21:20 Comment(0)
M
0

Dialog uses ContextThemeWrapper

Now, exception we are getting...

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'

To get rid of this just use proper inflater

LayoutInflater.from(context) instead

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

  2. getLayoutInflater()

Avoid void setContentView(int layoutResID) instead use void setContentView(View view)

And use same context in Dialog constructor i.e super(context)

At last please don't forget to define android:onClick="method" in Activity instead in your custom class

Meeks answered 8/10, 2014 at 18:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.