当前位置: 动力学知识库 > 问答 > 编程问答 >

android - NullPointerException at getView method in Adapter

问题描述:

I'm trying to populate a ListView from a SQLite database. When I scroll to the ListView the application crashed and I get a NullPointerException in getView() in the adapter class. How to solve this issue?

Getting NullPointerException at this line:

holder.txtInIt.setText(followUp_List.getShortRGN());

Here is my adapter code.

@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;

}

}

Here is the logcat information:

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)

网友答案:

The holder object is null. Hence the exception. So if holder is null, you need to initialize the holder.

Instead of if (row == null) , use if (row == null || holder == null)

Also, inside youif condition, you need to add row.setTag(holder). This is important, only then you can perform the getTag().

So it will be something like this:

            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();
            }
网友答案:

Please check your layout file, the same you have implemented to your adapter class and after that check your textview id is the same that you have added to your adapter class. I thought your textview id is different as the adapter class.

Just replace the below code into your adapter class

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();
            }
分享给朋友:
您可能感兴趣的文章:
随机阅读: