当尝试在自定义ListAdapter中设置一个TextView时,应用程序强制关闭

时间:2012-01-09 10:09:44

标签: android textview android-arrayadapter custom-adapter

我想在TexView中设置文本,实际上我将数字存储在数组列表中,但它始终强制关闭。所以,我尝试手动设置,但它仍然强制关闭。我不知道为什么其他textview能够设置但是,amountStr无法设置。

提前感谢您的建议。

这里有一些代码:

ActActivity.class

public class ActActivity extends ListActivity {

    private CustomListAdapter listAdapter;
    private String[] activities_list;

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

        activities_list = getResources().getStringArray(R.array.Activities_array);

        listAdapter = new CustomListAdapter(this,R.layout.list_item_choose_activities,activities_list);      

        setListAdapter(listAdapter);

    } 

    private class CustomListAdapter extends ArrayAdapter<String>{

        private String[] items;
        public CustomListAdapter(Context context, int textViewResourceId,
                String[] items) {
            super(context, textViewResourceId, items );
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if(v == null){
                LayoutInflater  vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_item_choose_activities, null);
            }

            TextView actNameStr = (TextView)v.findViewById(R.id.list_act_txt);
            ImageView actLogo = (ImageView)v.findViewById(R.id.act_logo);
            TextView amountStr = (TextView)findViewById(R.id.amount_text);

            actNameStr.setText(items[position]);
            // i wanna set text number here, actually i store number in array list but,it always force close. so, i try just set manually though, it still force close.
            amountStr.setText("5");


            //Set icon of activity on each list items
            switch (position) {
            case 0:
                actLogo.setBackgroundResource(R.drawable.a01_ofroad);
                break;
            case 1:
                actLogo.setBackgroundResource(R.drawable.a02_bungeejump);
                break;
            case 2:
                actLogo.setBackgroundResource(R.drawable.a03_art);
                break;
                        default:
                actLogo.setBackgroundResource(R.drawable.nologo);
                break;
            }   
        return v;
    }

  }
}

list_item_choose_activities.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<ImageView
    android:id="@+id/act_logo" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="5sp"
    android:background="@drawable/nologo"
    />
<TextView 
    android:id="@+id/list_act_txt" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/act_logo"
    android:textColor="#494644"
    android:padding="15dp"
    android:textSize="15sp"
    android:layout_gravity="left"
    android:textStyle="bold"
   >    
</TextView>
<LinearLayout 
    android:id="@+id/list_bubble"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_gravity="center"
    android:background="@drawable/list_bubble_num">
    <TextView 
    android:id="@+id/amount_text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:textSize="15sp"
    android:textStyle="bold"
    android:textColor="#FFFFFF"
    android:text="0"
    />
</LinearLayout>

</RelativeLayout>

logcat的

01-09 16:38:47.051: E/AndroidRuntime(21240): FATAL EXCEPTION: main
01-09 16:38:47.051: E/AndroidRuntime(21240): java.lang.NullPointerException
01-09 16:38:47.051: E/AndroidRuntime(21240):    at com.april.travel.ActActivity$CustomListAdapter.getView(ActActivity.java:181)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.AbsListView.obtainView(AbsListView.java:1430)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.ListView.onMeasure(ListView.java:1127)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.View.measure(View.java:8313)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.View.measure(View.java:8313)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.View.measure(View.java:8313)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.View.measure(View.java:8313)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.View.measure(View.java:8313)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.View.measure(View.java:8313)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.View.measure(View.java:8313)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.View.measure(View.java:8313)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.View.measure(View.java:8313)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.View.measure(View.java:8313)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.View.measure(View.java:8313)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.os.Looper.loop(Looper.java:130)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at android.app.ActivityThread.main(ActivityThread.java:3683)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at java.lang.reflect.Method.invokeNative(Native Method)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at java.lang.reflect.Method.invoke(Method.java:507)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 16:38:47.051: E/AndroidRuntime(21240):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

TextView amountStr = (TextView)findViewById(R.id.amount_text);也许您需要更改为: TextView amountStr = (TextView)v.findViewById(R.id.amount_text);

答案 1 :(得分:0)

您是否遗漏了对

v 的引用

TextView amountStr =(TextView)findViewById(R.id.amount_text);

不应该是

TextView amountStr =(TextView)v.findViewById(R.id.amount_text);