我正在尝试从 Application 类中定义的 ArrayList 中显示 ListView。但是,每当我在 Logcat 中没有给出原因的情况下滚动时,我都会遇到 NullPointerException。
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 中。
对此的任何帮助将不胜感激。谢谢!