我正在尝试从SQLite数据库填充ListView。当我滚动到ListView时,应用程序崩溃了,我在适配器类的NullPointerException
中得到getView()
。如何解决这个问题?
在此行获取NullPointerException
:
holder.txtInIt.setText(followUp_List.getShortRGN());
这是我的适配器代码。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final Holder holder;
if (row == null)
{
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
row = vi.inflate(R.layout.follow_uplist_item, parent, false);
holder = new Holder();
holder.txtInIt = (TextView) row.findViewById(R.id.txtInitialLetter);
holder.txtUserName = (TextView) row.findViewById(R.id.txt_UserName);
holder.txtDate = (TextView) row.findViewById(R.id.txt_followDate);
holder.txtFolloUp = (TextView) row.findViewById(R.id.textFolloup);
}
else
{
holder = (Holder) row.getTag();
}
final FollowUp_List followUp_List = data.get(position);
holder.txtInIt.setText(followUp_List.getShortRGN());
if (holder.txtInIt.getText().toString().equals("R"))
{
holder.txtInIt.setBackgroundResource(R.drawable.red_circle_shape);
}
if (holder.txtInIt.getText().toString().equals("G")) {
holder.txtInIt.setBackgroundResource(R.drawable.green_circle_shape);
}
if (holder.txtInIt.getText().toString().equals("N")) {
holder.txtInIt.setBackgroundResource(R.drawable.blue_circle_shape);
}
holder.txtUserName.setText(followUp_List.getCreatedBy());
holder.txtDate.setText(followUp_List.getFollowup_date());
holder.txtFolloUp.setText(followUp_List.getFollowUp());
return row;
}
final class Holder {
TextView txtInIt;
TextView txtUserName;
TextView txtDate;
TextView txtFolloUp;
}
}
这是logcat信息:
10-24 09:54:06.057 5826-5826/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.tazeen.classnkk.FollowUpList$FollowUpListAdapter.getView(FollowUpList.java:299)
at android.widget.AbsListView.obtainView(AbsListView.java:2012)
at android.widget.ListView.makeAndAddView(ListView.java:1772)
at android.widget.ListView.fillDown(ListView.java:672)
at android.widget.ListView.fillGap(ListView.java:636)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4546)
at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:2852)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3106)
at android.view.View.dispatchTouchEvent(View.java:5486)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1714)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1892)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1840)
at android.view.View.dispatchPointerEvent(View.java:5662)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2863)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
holder
对象为空。因此例外。因此,如果holder
为空,则需要初始化holder
。
而不是if (row == null)
,请使用if (row == null || holder == null)
此外,在if
条件中,您需要添加row.setTag(holder)
。这很重要,只有这样才能执行getTag()
。
所以它会是这样的:
if (row == null || holder == null)
{
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
row = vi.inflate(R.layout.follow_uplist_item, parent, false);
holder = new Holder();
holder.txtInIt = (TextView) row.findViewById(R.id.txtInitialLetter);
holder.txtUserName = (TextView) row.findViewById(R.id.txt_UserName);
holder.txtDate = (TextView) row.findViewById(R.id.txt_followDate);
holder.txtFolloUp = (TextView) row.findViewById(R.id.textFolloup);
row.setTag(holder);
}
else
{
holder = (Holder) row.getTag();
}
答案 1 :(得分:0)
请检查您的布局文件,与您的适配器类相同,然后检查您的textview ID是否与添加到适配器类相同。我认为你的textview id与适配器类不同。
只需将以下代码替换为适配器类
即可if (row == null)
{
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
row = vi.inflate(R.layout.follow_uplist_item, parent, null);
holder = new Holder();
holder.txtInIt = (TextView) row.findViewById(R.id.txtInitialLetter);
holder.txtUserName = (TextView) row.findViewById(R.id.txt_UserName);
holder.txtDate = (TextView) row.findViewById(R.id.txt_followDate);
holder.txtFolloUp = (TextView) row.findViewById(R.id.textFolloup);
}
else
{
holder = (Holder) row.getTag();
}