0

This is my dialog_date_range.xml layout for the dialog:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:foo="http://schemas.android.com/apk/res/com.example.database_fragment"
    android:id="@+id/dialog_body"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:onClick="toggleDateRange"
        android:text="Button" />
</LinearLayout>

In my Activity I have:

public void toggleDateRange(View v) {
    if(dialog == null) {
        dialog = new Dialog(context, R.style.PauseDialogAnimation);
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.dialog_date_range);
        dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
    }

    if(dialog.isShowing()) {
        dialog.dismiss();
    } else {
        dialog.show();
    }
}

This is the error I get when I click on button:

FATAL EXCEPTION: main E/AndroidRuntime(25357): java.lang.IllegalStateException:   
Could not find a method toggleDateRange(View) in the activity class   
android.view.ContextThemeWrapper for onClick handler on view class   
android.widget.Button    
 with id 'button1'at android.view.View$1.onClick(View.java:3586) 
vovahost
  • 34,185
  • 17
  • 113
  • 116
  • Is your `toggleDateRange()` inside a listener, inner class, or something similar? I'm assuming you inflate the above layout and set it for the dialog? – codeMagic Jul 26 '13 at 14:47
  • OR if you really want to go that way the VIEW from setContentView is not the right one. I created a new project and added the exact same code from you,and it works. Check if you are in the view that you set the button. – MSA Jul 26 '13 at 14:57
  • this may sound silly but try cleaning the project and rebuilding? there's some funky errors sometimes with views if you don't clean – NujnaH Jul 26 '13 at 15:01
  • @codeMagic No, toggleDateRange() is inside my Activity. I updated the code. – vovahost Jul 26 '13 at 15:02
  • So, `dialog_date_range` is the xml you posted above? – codeMagic Jul 26 '13 at 15:07
  • Yes it's dialog_date_range – vovahost Jul 26 '13 at 15:14

2 Answers2

2

KISS - Keep It Simple Stupid (Sorry for the last word:P)

<Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:text="Button" />

implentent onClickListener

        Button btn1 = (Button)findViewById(R.id.button1);
        btn1.setonClickListener(this);

//implemented method

    @Override
    public void onClick(View v) {

      switch(v.getId){

         case R.id.button1:{

         //do here whatever you want
  }
 }
}

OR if you really want to go that way the VIEW from setContentView is not the right one. I created a new project and added the exact same code from you,and it works. Check if you are in the view that you set the button.

MSA
  • 2,502
  • 2
  • 22
  • 35
0

Android can't find the activity's methods inside the dialog according to this answer. So the solution would be to just set the onClickListener for the button: java.lang.illegalstateexception could not find a method (view) in the activity class android fragment

Change your code to be like this:

public void toggleDateRange(View v)
{
    if (dialog == null)
    {
        dialog = new Dialog(this, R.style.AppBaseTheme);
        dialog.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v)
            {
                toggleDateRange(v);

            }
        });
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.dialog_date_range);
        dialog.getWindow().getAttributes().windowAnimations = R.style.AppBaseTheme;
    }

    if (dialog.isShowing())
    {
        dialog.dismiss();
    }
    else
    {
        dialog.show();
    }
}
Community
  • 1
  • 1
tim.paetz
  • 2,635
  • 20
  • 23
  • In my case toggleDateRange() is inside my Activity class – vovahost Jul 26 '13 at 15:17
  • You need to set the onclicklistener for the button inside the dialog. The dialog cannot see the activity's method toggleDateRange so you won't be able to set it in xml for the dialog. See my code I added. – tim.paetz Jul 26 '13 at 15:29