尝试创建Dialog时应用程序崩溃

时间:2014-12-22 18:31:17

标签: java android dialog

我是新手。我的目的是在用户点击按钮时打开一个对话框。对话框应包含可编辑的文本区域,用户输入一些数据,以及“创建”和“取消”按钮。我通过XML将按钮链接到我的方法。但是,每当我运行它时,应用程序崩溃,只是说“(X App)已经停止”。

TerritoryList.java:

    /*Called upon when user clicks "Create new territory" button*/

    private void creationDialog (View v) {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Buisiness Call Creation");
    alert.setMessage("Create a new business call");

    //EditText view for user input
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Create", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface d, int whichButton) {
            String value = input.getText().toString();
            //Do something with the value
        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface d, int whichButton) {
            //Cancelled. Do nothing
        }
    });
}
}

这是我的activity_territory_list.xml(只是Button):

  <Button
    android:id="@+id/create_new_call"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="67dp"
    android:text="@string/create_territory"
    android:onClick="creationDialog" />

我已经接受了@ 323go的建议,这就是我认为正确的LogCat:

12-22 19:02:11.582: E/AndroidRuntime(2138): FATAL EXCEPTION: main
12-22 19:02:11.582: E/AndroidRuntime(2138): Process: com.example.buninessterritory1, PID: 2138
12-22 19:02:11.582: E/AndroidRuntime(2138): java.lang.IllegalStateException: Could not find a method creationDialog(View) in the activity class com.example.buninessterritory1.TerritoryList for onClick handler on view class android.widget.Button with id 'create_new_call'
12-22 19:02:11.582: E/AndroidRuntime(2138):     at android.view.View$1.onClick(View.java:3978)
12-22 19:02:11.582: E/AndroidRuntime(2138):     at android.view.View.performClick(View.java:4659)
12-22 19:02:11.582: E/AndroidRuntime(2138):     at android.view.View$PerformClick.run(View.java:19462)
12-22 19:02:11.582: E/AndroidRuntime(2138):     at android.os.Handler.handleCallback(Handler.java:733)
12-22 19:02:11.582: E/AndroidRuntime(2138):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-22 19:02:11.582: E/AndroidRuntime(2138):     at android.os.Looper.loop(Looper.java:146)
12-22 19:02:11.582: E/AndroidRuntime(2138):     at android.app.ActivityThread.main(ActivityThread.java:5692)
12-22 19:02:11.582: E/AndroidRuntime(2138):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 19:02:11.582: E/AndroidRuntime(2138):     at java.lang.reflect.Method.invoke(Method.java:515)
12-22 19:02:11.582: E/AndroidRuntime(2138):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
12-22 19:02:11.582: E/AndroidRuntime(2138):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
12-22 19:02:11.582: E/AndroidRuntime(2138):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

  

java.lang.IllegalStateException:在活动类com.example.buninessterritory1.TerritoryList中找不到方法creationDialog(View),用于视图类android.widget.Button上的onClick处理程序,其id为&#39; create_new_call&#39;

通过onClick XML属性调用的方法必须是public,而不是private