2017-01-09 88 views
-2

我想填充一個圖標和從一個有兩個這些項目的成員變量的類接收的文本的列表視圖。Android ListView與自定義適配器崩潰應用

每次在活動中顯示列表視圖時,應用程序都會崩潰,而我看不到它出了什麼問題。 我確定問題出在自定義數組適配器中,因爲我可以初始化並查看標準的listview沒有問題,但是我看不到這個問題在哪裏。

與信息類的定義如下:

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> 

我似乎無法得到列表視圖來根本就不是。它適用於我使用標準陣列適配器和簡單的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 
+0

發生崩潰,你遺漏了最重要的事情..崩潰轉儲/堆棧跟蹤。 – alzee

+0

分享你的故障記錄。這將極大地幫助 –

回答

0

在getView方法。 你必須使用rowView.findViewById。 不僅findViewById。 否則會得到空指針異常。

+0

就是這樣! 解決了這個問題,我不敢相信我錯過了! 非常感謝 – JDGale8

+0

好的沒問題。請將我的答案標記爲已接受 – maulik

相關問題