将标题添加到ListView时为空指针

时间:2014-12-29 22:46:21

标签: java android listview android-listview

所以,我想在ListView中的行之间添加Headers。我这样做只是为标题充气不同的布局。 ListView首次加载时,似乎没问题。但是当我滚动时,我会在随机时间在这一行上得到一个空指针:

tvHeader.setText(location.headerText);

仅当我将Headers添加到ListView时才会发生这种情况;如果我只添加行,那很好。感谢任何帮助。谢谢。

这是我的代码:

这是我的位置对象

package com.example.listview;


public class Location {
    public String name;
    public String details;
    public String distance;
    public String hours;
    public boolean header = false;
    public String headerText;

    public Location(String name, String details, String distance) {
        this.name = name;
        this.details = details;
        this.distance = distance;
        header = false;
    }

    public Location(String headerText) {
        this.headerText = headerText;
        header = true;
    }
}

以下是ListView的适配器

package com.example.listview;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class LocationAdapter extends ArrayAdapter<Location> {

    public LocationAdapter(Context context, ArrayList<Location> locations) {
        super(context, R.layout.item_location, locations);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        final Location location = getItem(position);

        if (!location.header) { // if it is NOT a header
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(
                        R.layout.item_location, parent, false); // inflate the row xml
            }

            TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
            TextView tvDetails = (TextView) convertView
                    .findViewById(R.id.tvDetails);
            TextView tvDistance = (TextView) convertView
                    .findViewById(R.id.tvDistance);

            // Populate the data into the template view using the data object
            tvName.setText(location.name);
            tvDetails.setText(location.details);
            tvDistance.setText(location.distance);

        } else if (location.header) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(
                        R.layout.listview_header, parent, false); // inflate the header xml
            }

            TextView tvHeader = (TextView) convertView
                    .findViewById(R.id.tvHeader);

            tvHeader.setText(location.headerText);

        }
        return convertView;
    }
}

以下是ListView

中一行的 item_location.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:listSelector="@drawable/listview_selector"
    android:minHeight="140dip"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tvDetails"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/tvDistance"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp" />

</LinearLayout>

以下是标题的 listview_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:listSelector="@drawable/listview_selector">

    <TextView
        android:id="@+id/tvHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp" />

</RelativeLayout>

最后,这是我的主要活动,我在这里添加行和标题:

package com.example.listview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

    LocationAdapter adapter;
    ArrayList<Location> arrayOfLocations;
    ListView listView;

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

        listView = (ListView) findViewById(R.id.listView1);

        arrayOfLocations = new ArrayList<Location>();

        for (int i = 0; i < 10; i++) {
            arrayOfLocations.add(new Location("August 9th")); // add a header
            arrayOfLocations.add(new Location("Best Fruit Stand Ever",
                    "Fruit!", "2 miles")); // add a row
        }

        // Create the adapter to convert the array to views
        adapter = new LocationAdapter(this, arrayOfLocations);

        listView.setAdapter(adapter);
    }
}

Logcat

12-29 17:36:22.091: E/AndroidRuntime(4930): java.lang.NullPointerException
12-29 17:36:22.091: E/AndroidRuntime(4930):     at com.example.listview.LocationAdapter.getView(LocationAdapter.java:53)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.widget.AbsListView.obtainView(AbsListView.java:2161)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.widget.ListView.makeAndAddView(ListView.java:1840)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.widget.ListView.fillDown(ListView.java:675)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.widget.ListView.fillGap(ListView.java:639)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4970)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3126)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3400)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.View.dispatchTouchEvent(View.java:7391)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2205)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1940)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2228)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1471)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2176)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.View.dispatchPointerEvent(View.java:7571)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:174)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5472)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5512)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.Choreographer.doFrame(Choreographer.java:530)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.os.Handler.handleCallback(Handler.java:730)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.os.Looper.loop(Looper.java:137)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at android.app.ActivityThread.main(ActivityThread.java:5289)
12-29 17:36:22.091: E/AndroidRuntime(4930):     at java.lang.reflect.Method.invok

1 个答案:

答案 0 :(得分:1)

使用convertViews时,表示您正在尝试回收视图。想象一下,当你进入getView并且该位置是一个标题时。

它将检查convertView并查看它不为null,因为它已被回收并且来自不是标题的行。

现在它将尝试从convertView获取R.id.tvHeader,但是没有R.id.tvHeader,因为布局膨胀是item_location布局。

如果它不为null,则必须检查convertView是否是膨胀的listview_header,或者改变你处理行的方式。