本文介绍了NullPointerException异常有关android.widget.AbsListView.obtainView(AbsListView.java:2275)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题搞清楚哪里开始调试。
这是来自logcat的信息:

  01-22 18:28:20.472:E / AndroidRuntime(10859):致命异常:主要
01-22 18:28:20.472:E / AndroidRuntime(10859):显示java.lang.NullPointerException
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.AbsListView.obtainView(AbsListView.java:2275)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.ListView.onMeasure(ListView.java:1156)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.View.measure(View.java:15172)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.View.measure(View.java:15172)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.View.measure(View.java:15172)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1447)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.View.measure(View.java:15172)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:617)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.View.measure(View.java:15172)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.View.measure(View.java:15172)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.View.measure(View.java:15172)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-22 18:28:20.472:E / AndroidRuntime(10859):在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2435)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.View.measure(View.java:15172)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4214)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.Choreographer.doFrame(Choreographer.java:525)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:711)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.os.Handler.handleCallback(Handler.java:615)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.os.Handler.dispatchMessage(Handler.java:92)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.os.Looper.loop(Looper.java:137)
01-22 18:28:20.472:E / AndroidRuntime(10859):在android.app.ActivityThread.main(ActivityThread.java:4931)
01-22 18:28:20.472:E / AndroidRuntime(10859):在java.lang.reflect.Method.invokeNative(本机方法)
01-22 18:28:20.472:E / AndroidRuntime(10859):在java.lang.reflect.Method.invoke(Method.java:511)
01-22 18:28:20.472:E / AndroidRuntime(10859):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:791)
01-22 18:28:20.472:E / AndroidRuntime(10859):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
01-22 18:28:20.472:E / AndroidRuntime(10859):在dalvik.system.NativeStart.main(本机方法)

正如你可以看到有没有信息avaliable有关与它我的应用程序包或堆栈跟踪

请帮忙

我的适配器code:

公共类MyChatGroupAdapter扩展ArrayAdapter {

 私人列表<项目>项目;
私人LayoutInflater吹气;
公共静态INT [] = LIST_TYPES {ChatActivity.VAL_MSG_TYPE_TEXT,
        ChatActivity.VAL_MSG_TYPE_IMAGE};//私人TreeSet中<整数GT; mSeparatorsSet =新TreeSet的<整数GT;();公共MyChatGroupAdapter(上下文的背景下,LayoutInflater充气,
        清单<项目>项目){
    超(背景下,0项);
    this.items =物品;
    this.inflater =吹气;
} @覆盖
公共无效添加(项目项目){
    // TODO自动生成方法存根
    super.add(项目);
    items.add(项目);
}@覆盖
    公众诠释getViewTypeCount(){
        //获取枚举的项目数
        返回LIST_TYPES.length;    }    @覆盖
    公众诠释getItemViewType(INT位置){
        //从项目使用的界面getViewType
        返回items.get(位置).getViewType();
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        //从项目使用的界面getView
        返回items.get(位置).getView(吹气,convertView);
    }    公共无效添加(TextItem项目,布尔addInStart){
        // TODO自动生成方法存根
        如果(addInStart)
        items.add(0项);
        其他
            items.add(项目);        notifyDataSetChanged();
    }    公共无效添加(的ImageItem项,布尔addInStart){
        // TODO自动生成方法存根
        如果(addInStart)        items.add(0项);
        其他
            items.add(项目);        notifyDataSetChanged();
    }

而这正是我填充的ListView:

  @覆盖
公共无效onLoadFinished(装载机<&光标GT;装载机,光标CUR){
    // TODO自动生成方法存根    尝试{
    如果(myChatGroupAdapter == NULL){
        myChatGroupAdapter =新MyChatGroupAdapter(getActivity()
                getActivity()。getLayoutInflater(),
                FragmentOneChatHistory.parseChatItemsFromCursor(现));
        Log.w(FragmentTopFansWithWHeel,onLoadFinished:适配器为空);
    }其他{        myChatGroupAdapter.clear();
        myChatGroupAdapter.addAll(FragmentOneChatHistory.parseChatItemsFromCursor(现));
        Log.w(FragmentTopFansWithWHeel
                onLoadFinished:更改适配器项目);    }
    this.handler.post(新PopulateArtistGroupListView());
    }赶上(例外五)
    {
        e.printStackTrace();    }
}

当我尝试调试它,它在执行此行后粉碎:
this.handler.post(新PopulateArtistGroupListView());

但我不能让里面PopulateArtistGroupListView断点

私有类PopulateArtistGroupListView实现Runnable {

  @覆盖
    公共无效的run(){
        ListView控件listRecentPlayed =(ListView控件)getActivity()。findViewById(
                R.id.listViewArtistGroup);
        listRecentPlayed.setAdapter(myChatGroupAdapter);
        listRecentPlayed
                .setOnItemClickListener(FragmentTopFansWithWHeel.this);    }}


解决方案

没有人回答,但我找到了解决办法。 debuging(很多错误的地方太)一周后,我发现我的自定义getItemViewType返回-1,是什么原因造成的AbsListView NullPointerException异常

i have a problem figure out where to start debugging.this is the information from logcat:

01-22 18:28:20.472: E/AndroidRuntime(10859): FATAL EXCEPTION: main
01-22 18:28:20.472: E/AndroidRuntime(10859): java.lang.NullPointerException
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.AbsListView.obtainView(AbsListView.java:2275)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.ListView.onMeasure(ListView.java:1156)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.View.measure(View.java:15172)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.View.measure(View.java:15172)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.View.measure(View.java:15172)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1447)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.View.measure(View.java:15172)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.View.measure(View.java:15172)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.View.measure(View.java:15172)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.View.measure(View.java:15172)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2435)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.View.measure(View.java:15172)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.Choreographer.doFrame(Choreographer.java:525)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.os.Handler.handleCallback(Handler.java:615)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.os.Looper.loop(Looper.java:137)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at android.app.ActivityThread.main(ActivityThread.java:4931)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at java.lang.reflect.Method.invokeNative(Native Method)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at java.lang.reflect.Method.invoke(Method.java:511)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
01-22 18:28:20.472: E/AndroidRuntime(10859):    at dalvik.system.NativeStart.main(Native Method)

As you can see there is no information avaliable about my app package or stack trace related to it

Please help

My Adapter code:

public class MyChatGroupAdapter extends ArrayAdapter {

private List<Item> items;
private LayoutInflater inflater;
public static int[] LIST_TYPES = { ChatActivity.VAL_MSG_TYPE_TEXT,
        ChatActivity.VAL_MSG_TYPE_IMAGE };

// private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

public MyChatGroupAdapter(Context context, LayoutInflater inflater,
        List<Item> items) {
    super(context, 0, items);
    this.items = items;
    this.inflater = inflater;
}



 @Override
public void add(Item item) {
    // TODO Auto-generated method stub
    super.add(item);
    items.add(item);
}



@Override
    public int getViewTypeCount() {
        // Get the number of items in the enum
        return LIST_TYPES.length;

    }

    @Override
    public int getItemViewType(int position) {
        // Use getViewType from the Item interface
        return items.get(position).getViewType();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Use getView from the Item interface
        return items.get(position).getView(inflater, convertView);
    }

    public void add(TextItem item,boolean addInStart) {
        // TODO Auto-generated method stub
        if(addInStart)
        items.add(0,item);
        else
            items.add(item);

        notifyDataSetChanged();
    }

    public void add(ImageItem item,boolean addInStart) {
        // TODO Auto-generated method stub
        if(addInStart)

        items.add(0,item);
        else
            items.add(item);

        notifyDataSetChanged();


    }

And this is where i populate the listview:

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cur) {
    // TODO Auto-generated method stub

    try{
    if (myChatGroupAdapter == null) {
        myChatGroupAdapter = new MyChatGroupAdapter(getActivity(),
                getActivity().getLayoutInflater(),
                FragmentOneChatHistory.parseChatItemsFromCursor(cur));
        Log.w("FragmentTopFansWithWHeel", "onLoadFinished: adapter was null");
    } else {

        myChatGroupAdapter.clear();
        myChatGroupAdapter.addAll(FragmentOneChatHistory.parseChatItemsFromCursor(cur));
        Log.w("FragmentTopFansWithWHeel",
                "onLoadFinished: change adapter items");



    }


    this.handler.post(new PopulateArtistGroupListView());
    }catch(Exception e)
    {
        e.printStackTrace();

    }


}

when i try to debug it it crushes after executing this line:this.handler.post(new PopulateArtistGroupListView());

but i cant get to the break point inside PopulateArtistGroupListView

private class PopulateArtistGroupListView implements Runnable {

    @Override
    public void run() {
        ListView listRecentPlayed = (ListView) getActivity().findViewById(
                R.id.listViewArtistGroup);
        listRecentPlayed.setAdapter(myChatGroupAdapter);
        listRecentPlayed
                .setOnItemClickListener(FragmentTopFansWithWHeel.this);

    }

}
解决方案

No One Answered but i found the solution. After one week of debuging (a lot in wrong places too) i found out that my custom getItemViewType returned -1, what caused a NullPointerException in AbsListView

这篇关于NullPointerException异常有关android.widget.AbsListView.obtainView(AbsListView.java:2275)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:48