2014-12-22 65 views
-1

我是一個新手。我的目的是在用戶點擊一個按鈕時產生一個對話框。對話框應該包含用戶輸入一些數據的可編輯文本區域,以及「創建」和「取消」按鈕。我通過XML將按鈕鏈接到了我的方法。然而,每次我運行它的應用程序崩潰,只是說「(X應用程序)已停止」。嘗試創建對話框時應用程序崩潰

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 
    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) 
+2

從所提供的代碼塊中未提出異常 –

+0

此處解決了類似的問題。 試試這個 http://stackoverflow.com/questions/5618664/an-established-connection-was-aborted-by-the-software-in-your-host-machine – Rohit5k2

+0

@ Rohit5k2一點不回答我的問題... –

回答

2

java.lang.IllegalSta teException:找不到在活動課com.example.buninessterritory1.TerritoryList爲的onClick處理程序的方法creationDialog(視圖)和視圖類android.widget.Button ID爲「create_new_call」

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

+0

好的,非常感謝,它不再崩潰。然而,一旦點擊,就沒有活動......對話框不會出現 –

+0

您還需要添加'alert.create();'和'alert.show()'。就目前而言,按下「創建」按鈕時,對話框既不會創建也不會顯示。 – Willis

+0

啊,這太好了,謝謝!然而,標題,消息和TextView顯示,但至於我的2個按鈕,那裏不存在.... –

相關問題