ListView崩溃

时间:2015-01-21 12:45:55

标签: android listview

我随机崩溃了。 我不知道原因,因为日志报告没有显示任何行号。 我想我在列表视图中遇到了这个崩溃。

我已将此列表视图附加在相对布局下方。我根据记录设置了listview的可见性。

STACK_TRACE=java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setDrawingCacheBackgroundColor(int)' on a null object reference
at android.widget.AbsListView.obtainView(AbsListView.java:2357)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17430)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17430)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

请帮助我摆脱这次崩溃。

代码:

searchEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().length() > 0) {
                    placesTask = new PlacesTask();
                    placesTask.execute(s.toString());
                }
            }
        });

private class ParserTask extends
            AsyncTask<String, Integer, List<TextSearchPlace>> {

        JSONObject jObject;

        @Override
        protected List<TextSearchPlace> doInBackground(String... jsonData) {

            List<TextSearchPlace> places = null;

            PlaceJSONParser placeJsonParser = new PlaceJSONParser(getActivity());

            try {
                jObject = new JSONObject(jsonData[0]);

                places = placeJsonParser.parse(jObject);

            } catch (Exception e) {
                Log.d("Exception", e.toString());
            }
            return places;
        }

        @Override
        protected void onPostExecute(List<TextSearchPlace> result) {
            autocompletePlaces = result;
            if (searchLayout.getVisibility() == View.VISIBLE) {
                try {
                    if (result != null && result.size() > 0) {
                        rootView.findViewById(R.id.dummy_iv).setVisibility(
                                View.VISIBLE);
                        suggestionLv.setAdapter(new PlaceSuggestionListAdapter(
                                getActivity(), result));
                        suggestionLv.setVisibility(View.VISIBLE);
                    } else {
                        rootView.findViewById(R.id.dummy_iv).setVisibility(
                                View.GONE);
                        suggestionLv.setVisibility(View.GONE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

如果没有找到结果,我正在显示列表,如果我得到结果和隐藏列表。

我的适配器 -

public class PlaceSuggestionListAdapter extends BaseAdapter {

    Context mContext;
    List<TextSearchPlace> places;

    public PlaceSuggestionListAdapter(Context mContext,
            List<TextSearchPlace> places) {
        this.mContext = mContext;
        this.places = places;
    }

    @Override
    public int getCount() {
        return places.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (mContext != null) {
            if (convertView == null) {
                convertView = ((Activity) mContext).getLayoutInflater()
                        .inflate(R.layout.place_list_item, null);

                holder = new ViewHolder();
                holder.nameTv = (TextView) convertView
                        .findViewById(R.id.name_tv);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            UtilFunction.labelTypeFace(holder.nameTv, mContext.getAssets());
            holder.nameTv.setText(places.get(pos).getName());
        }

        return convertView;
    }

    private static class ViewHolder {
        public TextView nameTv;
    }

}

3 个答案:

答案 0 :(得分:2)

我怀疑你在getView中返回一个空视图,mContext可能为null。不要存储对Context的引用。在适配器的getView()中使用View.getContext()。

答案 1 :(得分:1)

你解决了这个问题吗? 我得到了完整的save logcat转储。 在我的情况下,我尝试在适配器使用的arraylist中有超过3个条目。尝试改为不同类型的布局。没有运气。

但是我在5分钟前发现了自己非常非常尴尬的错误。 我的Adapter.getView()返回null(剩下三天的调试测试行...)但是logcat从Main开始并在AbsListView.obtainView()停止,因此没有太多指向我的getView()。

答案 2 :(得分:1)

这是需要仔细审查问题。 我有这个问题的原因。

getView()返回null。