Using Onclick Attribute in Layout Xml Causes a Nosuchmethodexception in Android Dialogs

Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs

Define the method (dialogClicked) in Activity.
And modify TestDialog like the following code:

public class TestDialog extends Dialog {
Context mContext;
public TestDialog(final Context context)
{

super(context);
mContext=context;
}

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

I think it works :)

Android Dialog NoSuchMethodException error when using XML onClick

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.

Android Dialog NoSuchMethodException error when using XML onClick

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.

Android custom dialog button XML onClick error

If I understand your question correctly, you should be able to extend the Dialog class to do what you want, as in Jett's answer here: Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs

Could not find a method in android app

You paintClicked() method is not found as the layout is inflated in the dialog, and it looks for the method in there, but your method is in your activity.

You'll find an answer here:

Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs

android DialogFragment android:onClick=buttonCancel causes IllegalStateException could not find a method

I would try a different approach which works fine for me:

  1. implement an OnClickListener into your fragment:

    public class DialogNewDatabase extends DialogFragment implements OnClickListener`
  2. have a button with an unique id in xml, which does NOT need android:clickable

    <Button  android:id="@+id/dialog_new_database_button_cancel" />
  3. override the method onClick() within your fragment and insert a reaction on your click:

    public void onClick(View v) {       
    switch (v.getId()) {
    case R.id.dialog_new_database_button_cancel:
    // your stuff here
    this.dismiss();

    break;
    default:
    break;
    }
    }
  4. import the necessary:

    import android.view.View.OnClickListener; 
  5. start the onClickListener on the button:

    private Button bCancel = null;
    bCancel = (Button) findViewById(R.id.dialog_new_database_button_cancel);
    bCancel.setOnClickListener(this);
    // it is possible that you might need the refrence to the view.
    // replace 2nd line with (Button) getView().findViewById(...);

    This way you can handle even more clickable buttons in the same onClick-method. You just need to add more cases with the proper ids of your clickable widgets.



Related Topics



Leave a reply



Submit