簡體   English   中英

嘗試創建對話框時應用崩潰

[英]App crashing when trying to create 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)

java.lang.IllegalStateException:在活動類com.example.buninessterritory1.TerritoryList中為ID為'create_new_call'的視圖類android.widget.Button上的onClick處理程序找不到方法creationDialog(View)

通過onClick XML屬性調用的方法必須是public ,而不是private

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM