Android ListView与自定义适配器崩溃的应用程序

时间:2017-01-09 18:14:39

标签: java android listview custom-adapter

我正在尝试使用图标和一段文本填充列表视图,该文本是从具有这两个项目的成员变量的类中接收的。

每次在活动中显示列表视图时,应用都会崩溃,我无法看到它出了什么问题。 我确定问题出在自定义数组适配器中,因为我可以初始化并查看标准列表视图没问题,但我无法看到这个问题所在。

具有信息的类定义如下:

public class Pokemon {
// core info
String  name;
String  number;
int     no;
int     icon;


public Pokemon(String name, int n, int icon){
    this.name = name;
    this.number = Integer.toString(n);        
    this.no = n;
    this.icon = icon;
}

并在下面实例化为ArrayList:

ArrayList<Pokemon> starters = new ArrayList<>();

starters.add(new Pokemon("Bulbasaur", 1, R.drawable.p1)
    );
    starters.add(new Pokemon("Ivysaur", 2, R.drawable.p2)
    );
    starters.add(new Pokemon("Venasaur", 3, R.drawable.p3)
    );
    starters.add(new Pokemon("Charmander", 4, R.drawable.p4)
    );
    starters.add(new Pokemon("Charmeleon", 5, R.drawable.p5)
    );
    starters.add(new Pokemon("Charizard", 6, R.drawable.p6)
    );
    starters.add(new Pokemon("Squirtle", 7, R.drawable.p7)
    );
    starters.add(new Pokemon("Wartortle", 8, R.drawable.p8)
    );
    starters.add(new Pokemon("Blastoise", 9, R.drawable.p9)
    );

我的自定义适配器是:

    public class PokedexArrayAdaptor extends ArrayAdapter<Pokemon>{
    Context context;

    List<Pokemon> pokemonList;
    int layoutResourceId;

    public PokedexArrayAdaptor(Context context, int layoutResourceId, ArrayList<Pokemon> objects) {
        super(context, layoutResourceId, objects);

        this.context            = context;
        this.layoutResourceId   = layoutResourceId;
        this.pokemonList        = objects;
    }

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

        View rowView = convertView;
        PokemonHolder holder;

        Pokemon pokemon = pokemonList.get(position);

        if(rowView == null){
            LayoutInflater vi;
            vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = vi.inflate(layoutResourceId, null);

            holder          = new PokemonHolder();
            holder.name     = (TextView)findViewById(R.id.pokemonNameList);
            holder.sprite   = (ImageView)findViewById(R.id.pokemonIconList);
            holder.desc     = (TextView)findViewById(R.id.secondLine);

            rowView.setTag(holder);
        }
        else holder = (PokemonHolder)rowView.getTag();

        Pokemon pok = pokemonList.get(position);
        holder.name.setText(pok.name);
        holder.sprite.setImageResource(pokemon.icon);
        holder.desc.setText(pok.number);

   return rowView;
    }
}

在OnCreate中,我正在调用并设置适配器:

    customAdaptor = new PokedexArrayAdaptor(this, R.layout.pokedex_entry, starters);
    pokedexList = (ListView)findViewById(R.id.pokedexListView);
    pokedexList.setAdapter(customAdaptor);

我的ListView 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="?android:attr/listPreferredItemHeight"
            android:padding="6dip" >

<ImageView
    android:id="@+id/pokemonIconList"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:paddingRight="40dp"
    android:paddingLeft="20dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:contentDescription="TODO"
    android:src="@drawable/p1" />

<TextView
    android:id="@+id/secondLine"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="Description"
    android:textSize="12sp"
    android:layout_toRightOf="@id/pokemonIconList"/>

<TextView
    android:id="@+id/pokemonNameList"
    android:layout_width="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_height="wrap_content"
    android:layout_above="@id/secondLine"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignWithParentIfMissing="true"
    android:gravity="center_vertical"
    android:text="Example application"
    android:textSize="16sp"
    android:layout_toRightOf="@id/pokemonIconList"/>

我的活动xml是:

<?xml version="1.0" encoding="utf-8"?>
<dallasapps.shinyhunter.SlidingContainer
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">


<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFCDD2">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/pokedexListView">
    </ListView>

</LinearLayout>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainTrackScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="dallasapps.shinyhunter.Track"
    android:background="#E0E0E0"
    android:clickable="true">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/sideBtn"
        android:visibility="invisible"
        android:background="#0000"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Gen: ORAS"
        android:id="@+id/genTxt"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"/>


    <ImageButton
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:id="@+id/pokedexBtn"
        android:background="@drawable/pokedex_xy_icon"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_marginTop="74dp"
        android:layout_height="110dp"
        android:scaleType="fitCenter"
        android:layout_alignParentTop="true"
        android:src="@drawable/p8"
        android:id="@+id/trackedPokemon"
        android:layout_below="@+id/pokedexBtn"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30sp"
        android:text="#001 Bulbasaur"
        android:id="@+id/pokedexNo"
        android:gravity="center_vertical"
        android:layout_below="@+id/trackedPokemon"
        android:layout_centerHorizontal="true"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pokedexNo"
        android:layout_marginTop="15dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/linearLayout">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:textSize="50sp"
            android:text="-"
            android:id="@+id/minBtn"
            android:background="#F44336"
            android:layout_weight="1"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textSize="40dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="10dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="0"
                android:id="@+id/counterTxt"
                android:layout_weight="1"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Chance"
                android:id="@+id/chance"
                android:textAlignment="center"
                android:layout_below="@+id/linearLayout"
                android:layout_gravity="center_horizontal"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:textSize="30dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="1/4096"
                android:id="@+id/chanceTxt"
                android:layout_below="@+id/chance"
                android:layout_gravity="center"/>
        </LinearLayout>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:textSize="50sp"
            android:text="+"
            android:id="@+id/plusBtn"
            android:background="#F44336"
            android:layout_weight="1"
            />
    </LinearLayout>


    <Button
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="-15dp"
        android:scaleType="fitCenter"
        android:background="#0000"
        android:drawableBottom="@drawable/tallgrass"
        android:text="Method"
        android:id="@+id/methodBtn"
        style="?android:attr/borderlessButtonStyle"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/trackedPokemon"
        android:layout_alignEnd="@+id/trackedPokemon"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:textSize="24dp"
        android:text="Found"
        android:layout_marginBottom="30dp"
        android:id="@+id/foundBtn"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:id="@+id/scTrackChk"
        android:checked="false"
        android:layout_alignBottom="@+id/foundBtn"
        android:layout_alignLeft="@+id/methodBtn"
        android:layout_alignStart="@+id/methodBtn"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Shiny Charm"
        android:id="@+id/scTrackText"
        android:layout_below="@+id/scTrackChk"
        android:layout_alignRight="@+id/linearLayout"
        android:layout_alignEnd="@+id/linearLayout"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/imageView"
        android:src="@drawable/shiny_charm"
        android:layout_above="@+id/scTrackChk"
        android:layout_alignRight="@+id/scTrackChk"
        android:layout_alignEnd="@+id/scTrackChk"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/eggTrackImage"
        android:src="@drawable/egg"
        android:layout_above="@+id/mmTrackChk"
        android:layout_alignRight="@+id/mmTrackChk"
        android:layout_alignEnd="@+id/mmTrackChk"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="15dp"
        android:id="@+id/mmTrackChk"
        android:checked="false"
        android:layout_alignBaseline="@+id/scTrackChk"
        android:layout_alignBottom="@+id/scTrackChk"
        android:layout_alignRight="@+id/pokedexBtn"
        android:layout_alignEnd="@+id/pokedexBtn"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Masuda"
        android:id="@+id/masudaTrackTxt"
        android:layout_alignTop="@+id/scTrackText"
        android:layout_alignLeft="@+id/pokedexBtn"
        android:layout_alignStart="@+id/pokedexBtn"/>




</RelativeLayout>

我似乎无法填充listview。当我使用标准阵列适配器和一个简单的String[] list = {...}来填充它时,它可以正常工作,因此我相信它是我的自定义适配器的问题,但我无法看到它。

我已经尝试将持有者减少到仅处理文本值,并将其他值设置为每个列表值的常量,但这也不起作用。

任何想法出了什么问题?我在线跟踪了许多指南,尝试了不同的方法来覆盖ArrayAdapter,但它们都没有工作。

非常感谢任何帮助。

编辑: 崩溃日志:

01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: FATAL EXCEPTION: main
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: Process: dallasapps.shinyhunter, PID: 16121
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at dallasapps.shinyhunter.Track$PokedexArrayAdaptor.getView(Track.java:98)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.AbsListView.obtainView(AbsListView.java:2346)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.ListView.makeAndAddView(ListView.java:1876)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.ListView.fillDown(ListView.java:702)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.ListView.fillFromTop(ListView.java:763)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.ListView.layoutChildren(ListView.java:1685)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.AbsListView.onLayout(AbsListView.java:2148)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at dallasapps.shinyhunter.SlidingContainer.onLayout(SlidingContainer.java:70)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2684)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2178)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1938)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1114)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6068)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:670)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:606)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5539)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter D/AppTracker: App Event: crash

1 个答案:

答案 0 :(得分:0)

在getView方法中。 你必须使用rowView.findViewById。 不仅findViewById。 否则它将获得空指针异常。