有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当我想加载listview时,java Android ArrayAdapter崩溃

目标

我正在尝试用Android创建一个购物清单。它有一个活动类(MainActivity.java)和一个布局(activity_main.xml) 此程序具有用于添加项目的edittext和用于购物列表项目的listview

我使用GenyMoon在上运行mt程序。当我运行这个程序时,它完全崩溃了

结构

我的布局活动是主要的。xml类:

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:orientation="vertical">
    <EditText
        安卓:id="@+id/newItemText"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:hint="@string/addItemHint"
        安卓:contentDescription="@string/addItemContentDescription"
        />

    <ListView
        安卓:id="@+id/shoppingListView"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"></ListView>

</LinearLayout>

这是我的MainActivity类中的OnCreate方法

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Inflating the view
        setContentView(R.layout.activity_main);

        //References from the UI widgets
        ListView shoppingListView = (ListView) findViewById(R.id.shoppingListView);
        final EditText newShoppingItemText = (EditText) findViewById(R.id.newItemText);

        //the list of shopping items
        final ArrayList<String> shoppingListItems = new ArrayList<String>();
        shoppingListItems.add("Eggs");
        shoppingListItems.add("Bacon");
        //the array adapter, which binds the shopping list items array to the listview
        final ArrayAdapter<String> aa;
        aa = new ArrayAdapter<String>(MainActivity.this, R.layout.activity_main,shoppingListItems);

        //Bind the adapter to the listview
        shoppingListView.setAdapter(aa);

        newShoppingItemText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(event.getAction() == KeyEvent.ACTION_DOWN)
                    if( (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || (keyCode == KeyEvent.KEYCODE_ENTER) ) {
                        shoppingListItems.add(0, newShoppingItemText.getText().toString());
                        aa.notifyDataSetChanged();
                        newShoppingItemText.setText("");
                        return true;
                    }
                        return false;
            }
        });
    }

问题

当我在Genymotion中运行此代码时,我在logcat中得到以下错误输出:

Process: com.example.jonas.shoppinglist, PID: 8354 java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView at 安卓.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386) at 安卓.widget.ArrayAdapter.getView(ArrayAdapter.java:362) at 安卓.widget.AbsListView.obtainView(AbsListView.java:2255) at 安卓.widget.ListView.measureHeightOfChildren(ListView.java:1263) at 安卓.widget.ListView.onMeasure(ListView.java:1175) at 安卓.view.View.measure(View.java:16497)


共 (2) 个答案

  1. # 1 楼答案

    这行代码中的问题

    aa = new ArrayAdapter<String>(MainActivity.this, R.layout.activity_main,shoppingListItems);
    

    您不能在listview中将活动的布局作为单个项目的布局传递。请尝试以下方法:

    aa = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,shoppingListItems);