android崩溃中的自定义列表

时间:2012-12-05 13:16:16

标签: android android-listview

我创建了自定义列表但是当我运行它时,应用程序崩溃,它有什么问题,请告诉我。 从功能上来说,当我按下“+”按钮时,它会将EditText的计数增加一,当我按下“ - ”按钮时,它会将EditText的计数减一。  任何人告诉我这段代码有什么问题。创建自定义列表..

列出XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >



     <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

容器XML文件,其中包含按钮等

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="1kg" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="5" >

        <Button
            android:id="@+id/btnSubtract "
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="-" />

        <EditText
            android:id="@+id/etQuantity"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:ems="10"
            android:inputType="number" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/btnAdd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+" />
    </LinearLayout>

</LinearLayout>

主java类

package com.example.custoplist;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ListActivity {
    String[] itemsList= {"1/2 kg","1 kg", "5 kg", "10 kg", "16 kg"};
    String[] itemQuantity = new String[5];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,android.R.id.text1,itemsList));

    }
    private class MyAdapter extends ArrayAdapter<String>{

        public MyAdapter(Context context, int resource, int textViewResourceId,
                String[] objects) {
            super(context, resource, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            //return super.getView(position, convertView, parent);

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.content, parent, false);
            TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
            EditText etQuantity = (EditText) findViewById(R.id.etQuantity);
            Button btnAdd = (Button) findViewById(R.id.btnAdd);
            Button btnsSubtract = (Button) findViewById(R.id.btnSubtract);
            tvTitle.setText(itemsList[position]);   
            return row;
        }   
    }
}

1 个答案:

答案 0 :(得分:2)

更改这些行

        TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
        EditText etQuantity = (EditText) findViewById(R.id.etQuantity);
        Button btnAdd = (Button) findViewById(R.id.btnAdd);
        Button btnsSubtract = (Button) findViewById(R.id.btnSubtract);

        TextView tvTitle = (TextView) row.findViewById(R.id.tvTitle);
        EditText etQuantity = (EditText) row.findViewById(R.id.etQuantity);
        Button btnAdd = (Button) row.findViewById(R.id.btnAdd);
        Button btnsSubtract = (Button) row.findViewById(R.id.btnSubtract);