简体   繁体   中英

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. However, everytime I run it the app crashes, just saying "(X App) has stopped".

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):

  <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:

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'

Methods invoked via the onClick XML attribute must be public , not private .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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