安卓DialogFragment安卓的onClick =" buttonCancel"导致IllegalStateException异常无法找到一个方法 [英] android DialogFragment android:onClick="buttonCancel" causes IllegalStateException could not find a method

查看:184
本文介绍了安卓DialogFragment安卓的onClick =" buttonCancel"导致IllegalStateException异常无法找到一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的对话片段的一个问题。我想用机器人:onclick属性在我看来code更清楚再

I have a problem with my Dialog Fragment. I wanted to use android:onClick attribute as in my opinion code is more clear then.

在我的布局,我有以下声明:

In my layout I have the following declaration:

<Button
        android:id="@+id/dialog_new_database_button_cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_cancel"
        android:maxLines="1"
        style="?android:attr/buttonBarButtonStyle"
        android:onClick="buttonCancel"
        />

现在我DialogFragment

Now my DialogFragment

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DialogNewDatabase extends DialogFragment {

    public DialogNewDatabase() {
        // Empty constructor required for DialogFragment
        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView (inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.dialog_new_database, container);    
        getDialog().setTitle("Hello");
        return view;
    }

    @Override
    public void onCreate(Bundle bundle) {
        setCancelable(true);
        setRetainInstance(true);
        super.onCreate(bundle);

    }

    @Override
    public void onDestroyView() {
        if (getDialog() != null && getRetainInstance())
            getDialog().setDismissMessage(null);
        super.onDestroyView();
    }

    public void buttonCancel (View view) {
        dismiss();
    }

    public void buttonOK (View view) {

    }

}

我现在当我尝试点击取消按钮,我得到的:

I now when I try to click cancel button I get:

java.lang.IllegalStateException: Could not find a method buttonCancel(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'dialog_new_database_button_cancel'
at android.view.View$1.onClick(View.java:3031)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4482)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: buttonCancel [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getMethod(Class.java:915)
at android.view.View$1.onClick(View.java:3024)
... 11 more

你知道吗?是,也许是某种联系与我用的是进口android.support.v4.app.DialogFragment(支持V4)的事实?如何解决(我仍然会preFER使用安卓的onClick在XML布局)。

Any idea? Is that perhaps somehow related with the fact I use import android.support.v4.app.DialogFragment (support v4)? How to solve that (I still would prefer to use android:onClick in xml layout).

推荐答案

我会尝试不同的方法,它为我工作得很好:

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

  1. 实施OnClickListener到您的片段:

  1. implement an OnClickListener into your fragment:

public class DialogNewDatabase extends DialogFragment implements OnClickListener`

  • 有一个唯一的ID在XML中,一个按钮,做不会需要机器人:可点击

    <Button  android:id="@+id/dialog_new_database_button_cancel" />
    

  • 覆盖的方法的onClick()的片段中,并插入你的点击反应:

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

  • 导入必要的:

  • import the necessary:

    import android.view.View.OnClickListener; 
    

  • 开始onClickListener按钮:

  • 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(...);
    

    此方式,您可以处理在相同的onClick法更可点击的按钮。你只需要你点击小工具的正确的ID添加更多的情况。

    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.

    这篇关于安卓DialogFragment安卓的onClick =&QUOT; buttonCancel&QUOT;导致IllegalStateException异常无法找到一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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