gpt4 book ai didi

java - 向ListView添加标题时出现空指针

转载 作者:行者123 更新时间:2023-12-01 12:04:49 25 4
gpt4 key购买 nike

所以,我想在 ListView 中的行之间添加标题。我通过简单地为标题增加不同的布局来做到这一点。当 ListView 第一次加载时,看起来很好。但是当我滚动时,我会随机在该行上得到一个空指针:

tvHeader.setText(location.headerText);

只有当我向 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>

最后,这是我的主要 Activity ,我将在其中添加行和标题:

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

最佳答案

当您使用convertViews时,这意味着您正在尝试回收 View 。想象一下,当您进入 getView 内部并且位置是标题时。

它将检查convertView并发现它不为空,因为它被回收并且来自不是标题的行。

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

如果它不为空,您将必须检查convertView是否是膨胀的listview_header或更改处理行的方式。

关于java - 向ListView添加标题时出现空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27697127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com