alertDialog无法正常工作

时间:2014-03-25 18:22:50

标签: android compiler-errors android-alertdialog

按下某个按钮时,我正在尝试弹出警告对话框。我首先使用Android Developer的示例代码与'AlertDialog.Builder(getActivity());'而不是'AlertDialog.Builder(ConverteerRente.this);'这不起作用所以我根据我在这个网站上找到的内容改变了它但是现在我的程序被迫在按下之后停止按钮。

据您所知,这是在与主要不同的第二项活动中完成的。不确定这是否重要......

有人想让这个工作? 谢谢你的回答

public void HulpConverteer(){
             // 1. Instantiate an AlertDialog.Builder with its constructor
             AlertDialog.Builder builder = new AlertDialog.Builder(ConverteerRente.this);

             // 2. Chain together various setter methods to set the dialog characteristics
             builder.setMessage(R.string.dialog_message)
                    .setTitle(R.string.dialog_title);
            // 3. Get the AlertDialog from create()
            AlertDialog dialog = builder.create();
            dialog.show();
        }

'

crashlog:“03-25 19:34:24.373:E / AndroidRuntime(18828):致命异常:主要 03-25 19:34:24.373:E / AndroidRuntime(18828):java.lang.IllegalStateException:无法在视图类android上的onClick处理程序的活动类com.example.myapplication.ConverteerRente中找到方法HulpConverteer(View)。 widget.Button,id为'button3' 03-25 19:34:24.373:E / AndroidRuntime(18828):在android.view.View $ 1.onClick(View.java:3078) 03-25 19:34:24.373:E / AndroidRuntime(18828):在android.view.View.performClick(View.java:3558) 03-25 19:34:24.373:E / AndroidRuntime(18828):在android.view.View $ PerformClick.run(View.java:14157) 03-25 19:34:24.373:E / AndroidRuntime(18828):在android.os.Handler.handleCallback(Handler.java:605) 03-25 19:34:24.373:E / AndroidRuntime(18828):在android.os.Handler.dispatchMessage(Handler.java:92) 03-25 19:34:24.373:E / AndroidRuntime(18828):在android.os.Looper.loop(Looper.java:137) 03-25 19:34:24.373:E / AndroidRuntime(18828):在android.app.ActivityThread.main(ActivityThread.java:4514) 03-25 19:34:24.373:E / AndroidRuntime(18828):at java.lang.reflect.Method.invokeNative(Native Method) 03-25 19:34:24.373:E / AndroidRuntime(18828):at java.lang.reflect.Method.invoke(Method.java:511) 03-25 19:34:24.373:E / AndroidRuntime(18828):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790) 03-25 19:34:24.373:E / AndroidRuntime(18828):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 03-25 19:34:24.373:E / AndroidRuntime(18828):at dalvik.system.NativeStart.main(Native Method) 03-25 19:34:24.373:E / AndroidRuntime(18828):引起:java.lang.NoSuchMethodException:HulpConverteer [类android.view.View] 03-25 19:34:24.373:E / AndroidRuntime(18828):at java.lang.Class.getConstructorOrMethod(Class.java:460) 03-25 19:34:24.373:E / AndroidRuntime(18828):at java.lang.Class.getMethod(Class.java:915) 03-25 19:34:24.373:E / AndroidRuntime(18828):在android.view.View $ 1.onClick(View.java:3071) 03-25 19:34:24.373:E / AndroidRuntime(18828):... 11 more“

<Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/button2"
        android:layout_toRightOf="@+id/textViewTypeRente"
        android:text="@string/hulp" 
        android:onClick="HulpConverteer"/>

4 个答案:

答案 0 :(得分:2)

试试这个,它应该有效。

public void HulpConverteer(View view) {
    new AlertDialog.Builder(getApplicationContext())
        .setTitle(R.string.dialog_title)
        .setMessage(R.string.dialog_message)
        .setNegativeButton("Cancel", null)
        .setPositiveButton("Ok", null)
        .create()
        .show();
}

答案 1 :(得分:1)

第一行日志表明您是通过onClick属性从xml中的Button调用HulpConverteer方法。 所以将public void HulpConverteer()更改为public void HulpConverteer(View view)

答案 2 :(得分:0)

       public void HulpConverteer(Activity activityContext){
         // 1. Instantiate an AlertDialog.Builder with its constructor
         AlertDialog.Builder builder = new AlertDialog.Builder(activityContext);

         // 2. Chain together various setter methods to set the dialog characteristics
         builder.setMessage(R.string.dialog_message)
                .setTitle(R.string.dialog_title);
        // 3. Get the AlertDialog from create()
        AlertDialog dialog = builder.create();
        dialog.show();
    }

答案 3 :(得分:0)

错误说:

19:34:24.373: E/AndroidRuntime(18828): java.lang.IllegalStateException: Could not find a method HulpConverteer(View) in the activity class com.example.myapplication.ConverteerRente for onClick handler on view class android.widget.Button with id 'button3'

所以它期待一个View!试试这个......

public void HulpConverteer(View view) {
new AlertDialog.Builder(this)
    .setTitle(R.string.dialog_title)
    .setMessage(R.string.dialog_message)
    .setNegativeButton("Cancel", null)
    .setPositiveButton("Ok", null)
    .create()
    .show();
}

这与OnClickListener()的onClick(View v)方法相同;