0

我正在创建一个自定义 ListView。但我不断收到 NullPointerException,我不知道为什么。我在 stackOVerflow 上的另一个问题上看到一个有同样问题的人,他必须删除 if (itemView == null)。这解决了他的问题。但这对我不起作用...你们能帮帮我吗?

收藏活动

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class FavoriteActivity extends Activity {

private List<Favorite> favoriteItem = new ArrayList<Favorite>();

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

    populateFavoriteList();
    populateListview();

}

private void populateFavoriteList() {
    favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 1"));
    favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 2"));
    favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 3"));
    favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 4"));
    favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 5"));
    favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 6"));
    favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 7"));
    favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 8"));
}

private void populateListview() {
    ArrayAdapter<Favorite> adapter = new MyListAdapter();
    ListView list = (ListView) findViewById(R.id.favoriteList);
    Log.v("PASSED","found the list by ID");
    list.setAdapter(adapter);
    Log.v("PASSED","adapter setted");
}

private class MyListAdapter extends ArrayAdapter<Favorite> {
    public MyListAdapter() {
        super(FavoriteActivity.this, R.layout.favorite_row, favoriteItem);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // To be sure we have a view, because null is possible
        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(R.layout.favorite_row,
                    parent, false);
        }

        // Find the item to work with
        Favorite currentItem = favoriteItem.get(position);

        // Filling the View
        ImageButton thumbnail = (ImageButton) findViewById(R.id.favoriteThumbnail);
        thumbnail.setBackgroundResource(currentItem.getThumbnail());

        TextView name = (TextView) findViewById(R.id.favoriteName);
        name.setText(currentItem.getName());

        return itemView;

    }
  }
}

收藏夹.class

public class Favorite {

private int favoriteThumbnail;
private String favoriteName;    

public Favorite(int favoriteThumbnail, String favoriteName){
    super();
    this.favoriteThumbnail = favoriteThumbnail;
    this.favoriteName = favoriteName;
}

public int getThumbnail(){
    return favoriteThumbnail;
}

public String getName(){
    return favoriteName;
}
}

最喜欢的行.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="4" >

    <ImageButton 
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:contentDescription="thumbnail"
        android:src="@drawable/icon_camera"
        android:layout_weight="1"
        android:background="@null"
        android:id="@+id/favoriteThumbnail"
        />
<TextView
    android:id="@+id/favoriteName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:text="Name"
    android:textSize="20sp" />

<ImageButton 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="Direction"
    android:layout_weight="1"
    android:src="@drawable/icon_directions"
    android:background="@null"
    android:id="@+id/favoriteDirections"/>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="Delete from favorite" 
    android:layout_weight="1"
    android:src="@drawable/icon_delete"
    android:background="@null"
    android:id="@+id/favoriteDelete"/>

日志猫

05-07 19:40:41.450: V/PASSED(26227): found the list by ID
05-07 19:40:41.450: V/PASSED(26227): adapter setted
05-07 19:40:41.480: D/AndroidRuntime(26227): Shutting down VM
05-07 19:40:41.480: W/dalvikvm(26227): threadid=1: thread exiting with uncaught exception (group=0x4187e2a0)
05-07 19:40:41.500: E/AndroidRuntime(26227): FATAL EXCEPTION: main
05-07 19:40:41.500: E/AndroidRuntime(26227): java.lang.NullPointerException
05-07 19:40:41.500: E/AndroidRuntime(26227):    at com.example.spui.FavoriteActivity$MyListAdapter.getView(FavoriteActivity.java:68)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.widget.AbsListView.obtainView(AbsListView.java:2461)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.widget.ListView.onMeasure(ListView.java:1162)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.View.measure(View.java:15473)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.View.measure(View.java:15473)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5056)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.View.measure(View.java:15473)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.View.measure(View.java:15473)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5056)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2361)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.View.measure(View.java:15473)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1974)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1217)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1390)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1112)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4472)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.Choreographer.doFrame(Choreographer.java:525)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.os.Handler.handleCallback(Handler.java:615)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.os.Looper.loop(Looper.java:137)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at android.app.ActivityThread.main(ActivityThread.java:4898)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at java.lang.reflect.Method.invoke(Method.java:511)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-07 19:40:41.500: E/AndroidRuntime(26227):    at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

3

您不应该在其中使用活动视图,getView而是使用适配器提供的视图。

public View getView(int position, View convertView, ViewGroup parent) {

// stuff

ImageButton thumbnail = (ImageButton) itemView.findViewById(R.id.favoriteThumbnail);

注意. itemView_ findViewById活动视图没有任何带有 id 的内容favoriteThumbnail

编辑:当然,这应该应用于 中的所有视图getView,而不仅仅是thumbnail

于 2013-05-07T18:43:39.243 回答
2

代替 :

ImageButton thumbnail = (ImageButton) findViewById(R.id.favoriteThumbnail);
thumbnail.setBackgroundResource(currentItem.getThumbnail());

TextView name = (TextView) findViewById(R.id.favoriteName);
name.setText(currentItem.getName());

经过 :

ImageButton thumbnail = (ImageButton) itemView.findViewById(R.id.favoriteThumbnail);
thumbnail.setBackgroundResource(currentItem.getThumbnail());

TextView name = (TextView) itemView.findViewById(R.id.favoriteName);
name.setText(currentItem.getName());
于 2013-05-07T18:47:13.120 回答
1
 ImageButton thumbnail = (ImageButton)itemview.findViewById(R.id.favoriteThumbnail);
 TextView name = (TextView) itemview.findViewById(R.id.favoriteName);  

您必须使用 itemview 来查找 ID。你没有,因此你得到了 NPE

此外,您应该考虑使用视图支架来实现平滑滚动和性能。

检查以下链接中标题为“将视图对象保留在视图支架中”下的主题

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

也可以看看下面链接中的视频

https://www.youtube.com/watch?v=wDBM6wVEO70

于 2013-05-07T18:46:43.420 回答