2012-05-17 32 views
-1

我想顯示從應用程序類中定義的ArrayList ListView。然而,每當我在Logcat中給出沒有原因的滾動時,我最終會遇到NullPointerException異常。Android:列表視圖空指針異常無原因

logcat的輸出:

05-17 11:05:35.365: W/System.err(2173): java.lang.NullPointerException 
05-17 11:05:35.375: W/System.err(2173):  at in.net.maloo.HomeScreenListView$myAdapter.getView(HomeScreenListView.java:66) 
05-17 11:05:35.385: W/System.err(2173):  at android.widget.AbsListView.obtainView(AbsListView.java:1294) 
05-17 11:05:35.385: W/System.err(2173):  at android.widget.ListView.makeAndAddView(ListView.java:1727) 
05-17 11:05:35.395: W/System.err(2173):  at android.widget.ListView.fillDown(ListView.java:652) 
05-17 11:05:35.395: W/System.err(2173):  at android.widget.ListView.fillGap(ListView.java:623) 
05-17 11:05:35.395: W/System.err(2173):  at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944) 
05-17 11:05:35.405: W/System.err(2173):  at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2485) 
05-17 11:05:35.405: W/System.err(2173):  at android.os.Handler.handleCallback(Handler.java:587) 
05-17 11:05:35.405: W/System.err(2173):  at android.os.Handler.dispatchMessage(Handler.java:92) 
05-17 11:05:35.415: W/System.err(2173):  at android.os.Looper.loop(Looper.java:123) 
05-17 11:05:35.415: W/System.err(2173):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
05-17 11:05:35.415: W/System.err(2173):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-17 11:05:35.425: W/System.err(2173):  at java.lang.reflect.Method.invoke(Method.java:521) 
05-17 11:05:35.425: W/System.err(2173):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
05-17 11:05:35.425: W/System.err(2173):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
05-17 11:05:35.425: W/System.err(2173):  at dalvik.system.NativeStart.main(Native Method) 

代碼:

package in.net.maloo; 

import android.app.ListActivity; 
import android.content.Context; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

public class HomeScreenListView extends ListActivity { 

    private static class myAdapter extends BaseAdapter{ 
     private LayoutInflater mInflater; 

     public myAdapter(Context context){ 
      mInflater = LayoutInflater.from(context); 

     } 

     public int getCount() { 
      // TODO Auto-generated method stub 
      return myApp.feedLength(0); 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View conertView, ViewGroup parent) { 
      ViewHolder holder; 

      if (conertView == null) { 
       conertView = mInflater.inflate(R.layout.listview_homescreen, null); 
       holder = new ViewHolder(); 

       holder.text1 = (TextView) conertView.findViewById(R.id.hs_listview_symbol); 
       holder.text2 = (TextView) conertView.findViewById(R.id.hs_listview_price); 
       holder.text3 = (TextView) conertView.findViewById(R.id.hs_listview_unit); 
       holder.text4 = (TextView) conertView.findViewById(R.id.hs_listview_change); 
       holder.text5 = (TextView) conertView.findViewById(R.id.hs_listview_pcp); 
      } else { 

       holder = (ViewHolder) conertView.getTag(); 
      } 

      String com, ltp, volume, ltq, pcp; 
      com = myApp.getFuturesData(position, "Commodity", 0) + " - " + myApp.getFuturesData(position, "Expiry", 0); 
      ltp = myApp.getFuturesData(position, "LTP") + ""; 
      ltq = "LTQ: " + myApp.getFuturesData(position, "LTQ"); 
      volume = "Volume: " + myApp.getFuturesData(position, "Volume"); 
      pcp = "PCP: " + myApp.getFuturesData(position, "PCP"); 

      try { 

      holder.text1.setText(com); 
      Log.e("Position", com); 
      holder.text2.setText(ltp); 
      holder.text3.setText(volume); 
      holder.text4.setText(ltq); 
      holder.text5.setText(pcp); 
      return conertView; 
      } 
      catch (NullPointerException e){ 
       e.printStackTrace(); 
      } 
      finally { 
       return conertView; 
      } 
     } 

     static class ViewHolder { 
      TextView text1; 
      TextView text2; 
      TextView text3; 
      TextView text4; 
      TextView text5; 
     } 
    } 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setListAdapter(new myAdapter(this));   

} 
} 

listview_homescreen佈局:

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

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="75dip" > 

     <TextView 
      android:id="@+id/hs_listview_symbol" 
      android:layout_width="200dip" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_alignRight="@+id/hs_listview_change" 
      android:text="Large Text" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="@android:color/tertiary_text_light" 
      android:textSize="17dip" /> 

     <TextView 
      android:id="@+id/hs_listview_price" 
      android:layout_width="75dip" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:text="28,788" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textSize="15dip" /> 

     <TextView 
      android:id="@+id/hs_listview_unit" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:text="Vol" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

     <TextView 
      android:id="@+id/hs_listview_pcp" 
      android:layout_width="75dip" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:text="PCP" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

     <TextView 
      android:id="@+id/hs_listview_change" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:text="Small Text" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 
    </RelativeLayout> 

</LinearLayout> 

鴨父母,convertView.getTag()方法返回null由於某種原因,因此所有的問題。當我之前使用對myApp中定義的數組的直接引用時,相同的代碼正常工作,但後來我切換到使用持有不同數據類型的自定義類的ArrayList。

如果重要,ListView活動包含在包含3個選項卡的Tabhost中。

任何與此有關的幫助將不勝感激。謝謝!

+0

歡迎來到SO。如果下面的答案之一解決了您的問題,您應該接受它(點擊相應答案旁邊的複選標記)。這有兩件事。它可以讓每個人都知道你的問題已經得到解決,並且可以幫助你獲得幫助。如果你認爲某些東西真的有用,那麼你也可以加快它的速度(點擊答案旁邊的向上箭頭)。祝你好運! – Barak

+0

這是一個6歲的問題。如果你是從中世紀的時代投票的話,至少請發表一個理由。 – Puneet

回答

2
if (conertView == null) { 
    conertView = mInflater.inflate(R.layout.listview_homescreen, null); 
    holder = new ViewHolder(); 

    holder.text1 = (TextView) conertView.findViewById(R.id.hs_listview_symbol); 
    holder.text2 = (TextView) conertView.findViewById(R.id.hs_listview_price); 
    holder.text3 = (TextView) conertView.findViewById(R.id.hs_listview_unit); 
    holder.text4 = (TextView) conertView.findViewById(R.id.hs_listview_change); 
    holder.text5 = (TextView) conertView.findViewById(R.id.hs_listview_pcp); 
    **conertView.setTag(holder);** 
} else { 
    holder = (ViewHolder) conertView.getTag(); 
} 

你錯過了conertView.setTag(holder);

+0

完全錯誤... convertView.setTag(持有人)當你正在創建一個新的視圖(當convertView = null) – Barak

+0

對不起我的錯誤,而不是我寫的設置得到 –

+0

認爲可能是這樣,這就是爲什麼我沒有立即downvote它。 – Barak

1

您似乎缺少convertView.setTag(yourtag);,所以當然getTag()返回null。

只需在else前添加if (conertView == null)的末尾即可。