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

原文 标签 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)

最佳答案

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

相关文章:

android - 片段内的ViewPager无法正常工作

android - Android:在文本视图中添加缩放

android - Android TextView:如何在TextView中设置整行的背景颜色

android - 从活动返回后,列表视图未更新

java - 在Android中解析复杂的JSON

android - Google Play游戏排行榜自动关闭

android - 在Android中动态添加和删除TextView

java - 在Android和显示中减去两个值

java - 如何使ArrayAdapter遵循ViewHolder模式?

android - 在下载Android源代码时,我得到的只是一堆空文件夹