2014-12-22 66 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个按钮,那里不存在.... –

相关问题