Android ListView 数组适配器中的 NullPointerException

Android NullPointerException in ListView Array Adapter

当我 运行 使用 NullPointerException 我的应用程序崩溃时。当我 运行 应用程序处于调试模式时,我在报告的代码行中的所有变量上添加了观察者,并且它们在调试模式下都具有值。我可以跨过调试器中有问题的行并且它不会崩溃,但是当我 return 视图(根据调试器,它也不为空)时应用程序崩溃但没有错误它只是崩溃。

堆栈跟踪

FATAL EXCEPTION: main
    Process: com.team.app, PID: 17963
    java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.team.app.Adapters.EditableContentListAdapter$ViewHolder.value' on a null object reference
    at com.team.app.Adapters.EditableContentListAdapter.getView(EditableContentListAdapter.java:83)
    at android.widget.AbsListView.obtainView(AbsListView.java:2346)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1281)
    at android.widget.ListView.onMeasure(ListView.java:1188)
    at android.view.View.measure(View.java:18880)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18880)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18880)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
    at android.view.View.measure(View.java:18880)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
    at android.view.View.measure(View.java:18880)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.view.View.measure(View.java:18880)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18880)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2650)
    at android.view.View.measure(View.java:18880)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2104)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1220)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1456)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1111)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6017)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:606)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:5526)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

GetView() 已编辑

public EditableContentListAdapter(Context context, int layoutResourceId, List<AdvertContent> data) {
        super(context, layoutResourceId, data);
        mLayoutId = layoutResourceId;
        mContext = context;
        values = data;
        for (int i = 0; i < data.size(); ++i) {
            mIdMap.put(data.get(i), i);
        }
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    AdvertContent content = getItem(position);


    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewHolder viewHolder; // view lookup cache stored in tag
    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = inflater.inflate(mLayoutId, parent, false);
        viewHolder.value = (TextView) convertView.findViewById(R.id.section_value);
        viewHolder.title = (TextView) convertView.findViewById(R.id.section_title);
        viewHolder.icon = (ImageView) convertView.findViewById(R.id.section_icon);
        viewHolder.value.setText(values.get(position).getValue().toString());
        viewHolder.title.setText(values.get(position).getName());
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    // Populate the data into the template view using the data object

    if (content != null) {
        viewHolder.title.setText(content.getName()); // carshes on any of these 3 lines in normal run.
        viewHolder.value.setText(content.getValue().toString());
        viewHolder.title.setTextColor(Color.GRAY);
    }
    return convertView;//crashes here in debugmode

}

ViewHolder

public static class ViewHolder {
    TextView title;
    TextView value;
    ImageView icon;
}

变量我看了

none 在调试模式下报告为 null

viewHolder
viewHolder.title
content.getName()
viewHolder.title.getText().toString()
convertView

这一行的问题:

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

您还必须为 xml 布局充气。

convertView = inflater .inflate(R.layout.your_row_layout, parent, false);

您没有在 getView() 方法中指定 xml 布局。你只是在实例化 LayoutInflater

In your Inflater you need to give your Layout.

Also you need to set Tag to convertview.

convertView = inflater.inflate(mLayoutId, parent, false);
convertView.setTag(viewHolder);

您的视图持有者出错 class 因此它正在崩溃,因为您在创建时没有为视图持有者设置标签,因此请按以下方式更改您的 getView 方法。

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {


AdvertContent content = getItem(position);


LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
    viewHolder = new ViewHolder();
    convertView = inflater.inflate(mLayoutId, parent, false);
    viewHolder.value = (TextView) convertView.findViewById(R.id.section_value);
    viewHolder.title = (TextView) convertView.findViewById(R.id.section_title);
    viewHolder.icon = (ImageView) convertView.findViewById(R.id.section_icon);

    convertView.setTag(viewHolder);


} else {
    viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object

if (content != null) {
    viewHolder.title.setText(content.getName()); // carshes on any of these 3 lines in normal run.
    viewHolder.value.setText(content.getValue().toString());
    viewHolder.title.setTextColor(Color.GRAY);
}
return convertView;//crashes here in debugmode

}