Приложение принудительно закрывается при попытке установить один TextView на customListAdapter - PullRequest
1 голос
/ 09 января 2012

Я хочу установить текст в 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 ]

1 голос
/ 09 января 2012

TextView amountStr = (TextView)findViewById(R.id.amount_text); возможно, вам нужно изменить на: TextView amountStr = (TextView)v.findViewById(R.id.amount_text);

0 голосов
/ 09 января 2012

Отсутствует ли ссылка на v в

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

?

Не должно ли это быть

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

?

...