0

问题源于可单击的确定按钮。当我单击按钮时(在 EditTexts 中存在/不存在文本和数字的情况下)应用程序停止工作。我似乎无法找到此错误的来源。我已经仔细检查了正在初始化的变量,问题似乎并不源于那里。任何人都可以帮忙吗?

代码如下:

public void mInput(View view){
     final Dialog dialogManualInput = new Dialog(this);
     dialogManualInput.setContentView(R.layout.dialog_A);
     dialogManualInput.setTitle("Title");

     et1 = (EditText) dialogManualInput.findViewById(R.id.item);
     et2 = (EditText) dialogManualInput.findViewById(R.id.price);
     et3 = (EditText) dialogManualInput.findViewById(R.id.quantity);

     m_adapter = new ArrayAdapter<String>(this,R.layout.activity_A,m_listItems);
     checkoutList = (ListView)findViewById(R.id.listview);
     checkoutList.setAdapter(m_adapter);

     dialogManualInput.show();

     Button okButton = (Button) dialogManualInput.findViewById(R.id.ok);
     okButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        String input_1 = et1.getText().toString();
        String input_2 = et2.getText().toString();
        String input_3 = et3.getText().toString();
        int int_1 = Integer.valueOf(et3);
        int int_2 = Integer.valueOf(et2);
        String input = "("+et2+")  " + et1 +"   "+ (int_1*int_2);
        if((null!=input_1)&&(null!=input_2)&&(null!=input_3)){
           m_listItems.add(input);
           m_adapter.notifyDataSetChanged();
        }
        dialogManualInput.dismiss();
      }
  });
}

这是日志猫:

09-23 23:56:01.793    7464-7464/com.kwesimbia.management E/ArrayAdapter? You must supply a resource ID for a TextView
09-23 23:56:01.803    7464-7464/com.kwesimbia.management D/AndroidRuntime? Shutting down VM
09-23 23:56:01.803    7464-7464/com.kwesimbia.management W/dalvikvm? threadid=1: thread exiting with uncaught exception (group=0x40a99228)
09-23 23:56:01.843    7464-7464/com.kwesimbia.management E/AndroidRuntime?   FATAL EXCEPTION: main
    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
        at android.widget.AbsListView.obtainView(AbsListView.java:2046)
        at android.widget.ListView.makeAndAddView(ListView.java:1820)
        at android.widget.ListView.fillDown(ListView.java:672)
        at android.widget.ListView.fillFromTop(ListView.java:732)
        at android.widget.ListView.layoutChildren(ListView.java:1659)
        at android.widget.AbsListView.onLayout(AbsListView.java:1876)
        at android.view.View.layout(View.java:11425)
        at android.view.ViewGroup.layout(ViewGroup.java:4232)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
        at android.view.View.layout(View.java:11425)
        at android.view.ViewGroup.layout(ViewGroup.java:4232)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
        at android.view.View.layout(View.java:11425)
        at android.view.ViewGroup.layout(ViewGroup.java:4232)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
        at android.view.View.layout(View.java:11425)
        at android.view.ViewGroup.layout(ViewGroup.java:4232)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
        at android.view.View.layout(View.java:11425)
        at android.view.ViewGroup.layout(ViewGroup.java:4232)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1509)
        at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2498)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:4944)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
         at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
        ... 33 more
  09-23 23:56:01.873     239-1201/? E/EmbeddedLogger? App crashed! Process: com.kwesimbia.management
  09-23 23:56:01.873     239-1201/? E/EmbeddedLogger? App crashed! Package:    com.kwesimbia.management v1 (1.0)
  09-23 23:56:01.873     239-1201/? E/EmbeddedLogger? Application Label: abaawa
  09-23 23:56:01.873     239-1201/? W/ActivityManager? Force finishing activity          com.kwesimbia.management/.ArtActivity
4

2 回答 2

1

问题可能出在您的布局上。向我们展示这个 XML 文件“R.layout.activity_A”或查看它。

于 2013-09-23T21:24:26.320 回答
0

编辑1:

从Logcat问题看来是

 m_adapter = new ArrayAdapter<String>(this,R.layout.activity_A,m_listItems);
 checkoutList = (ListView)findViewById(R.id.listview);
 checkoutList.setAdapter(m_adapter);

您正在提供 m_listItems(我不知道这是什么)并且它正在寻找一个TextView的 id传递一个 textView 的 ID

 ArrayAdapter<String>(this,R.layout.activity_A,m_listItems);

就像是

 ArrayAdapter<String>(this,R.layout.activity_A,R.id.textView01);

可能的错误

这是做什么的,你直接把你的编辑文本与字符串连接起来,这里预期的可能是它应该是int_1et1.getText()input_1

    String input = "("+et2+")  " + et1 +"   "+ (int_1*int_2);

如果有任何未像往常一样为 et1、et2、et3初始化的NPE或

在这些中解析异常(如果你没有填写editText,那么它必须抛出异常)

    int int_1 = Integer.valueOf(et3);
    int int_2 = Integer.valueOf(et2);
于 2013-09-23T21:14:32.123 回答