2012-12-05 83 views
1

我創建自定義列表,但是當我運行這個,應用程序崩潰,它是什麼問題,請告訴我。 和我魔杖的功能,當我按下「+」按鈕,它增加了一個EditText的計數,當我按下「 - 」按鈕,然後它減少了一個EditText的計數.. 任何一個告訴我什麼是問題在於這段代碼。它創建自定義列表..在android崩潰的自定義列表

列表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; 
     } 
    } 
} 
+0

告訴我們logcat的錯誤 –

+0

錯誤,,「不幸custopList被採空」 –

回答

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); 
+1

感謝,它的工作... :) –

+0

不要忘記接受的答案:) –

+0

您可以接受在4分鐘的答案... abi到雅aa raha哈:( –

相關問題