简体   繁体   English

尝试创建对话框时应用崩溃

[英]App crashing when trying to create Dialog

I am a novice. 我是新手。 My intention is to bring about a dialogue box when the user clicks a button. 我的意图是在用户单击按钮时创建一个对话框。 The dialogue box is supposed to contain an editable text area where the user input some data, and a "Create", and "Cancel" button. 该对话框应该包含一个供用户输入一些数据的可编辑文本区域,以及一个“创建”和“取消”按钮。 I linked the button to my method via XML. 我通过XML将按钮链接到我的方法。 However, everytime I run it the app crashes, just saying "(X App) has stopped". 但是,每次我运行该应用程序时,它都会崩溃,只是说“(X App)已停止”。

TerritoryList.java: 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
        }
    });
}
}

Here is my activity_territory_list.xml (just the Button): 这是我的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" />

I have taken the advice of @323go, and here is what I THINK is the correct LogCat: 我已经接受@ 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)

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' java.lang.IllegalStateException:在活动类com.example.buninessterritory1.TerritoryList中为ID为'create_new_call'的视图类android.widget.Button上的onClick处理程序找不到方法creationDialog(View)

Methods invoked via the onClick XML attribute must be public , not private . 通过onClick XML属性调用的方法必须是public ,而不是private

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM