【问题标题】:Null Pointer Exception in ExpandableListView in .put().put() 中 ExpandableListView 中的空指针异常
【发布时间】:2014-11-09 16:14:51
【问题描述】:

在我的应用程序中,我需要显示当月和过去两个月的数据。我为此使用了可扩展的列表视图。但我得到一个空指针异常。

我的班级:

public class HistoryFragment extends Fragment {

    HashMap<String,List<String>> months_parent;
    List<String> child_list;
    ExpandableListView exp_list;
    monthAdapter adapter;
    View v;

    Calendar cal = Calendar.getInstance();
    int y = cal.get(Calendar.YEAR);
    SQLiteDatabase db;
    DbHelper dbhelper;

    public HistoryFragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        v = inflater.inflate(R.layout.fragment_history, container, false);
        exp_list = (ExpandableListView) v.findViewById(R.id.months);        

        String from[] = {DbHelper.NOTE, DbHelper.AMOUNT, DbHelper.MONTH, DbHelper.YEAR};
        dbhelper = new DbHelper(getActivity());
        db = dbhelper.getReadableDatabase();

        List<String> curr = new ArrayList<String>();
        List<String> currm1 = new ArrayList<String>();
        List<String> currm2 = new ArrayList<String>();

        int m = cal.get(Calendar.MONTH);
        int n = m;
        int last = m-2;
        String noteamt;
        double amt,totcurr=0.0,totcurrm1=0.0,totcurrm2=0.0;
        Cursor c = db.query(DbHelper.TABLE_NAME, from, null, null, null, null,DbHelper.YEAR+" desc,"+DbHelper.MONTH+" desc");
        c.moveToFirst();
        if(c!=null)
        {
            while(c.moveToNext() && m>=last)
            {
                m=Integer.parseInt(c.getString(c.getColumnIndex(DbHelper.MONTH)));
                amt = Double.parseDouble(c.getString(c.getColumnIndex(DbHelper.AMOUNT)));
                //noteamt = c.getString(c.getColumnIndex(DbHelper.NOTE))+"( "+c.getString(c.getColumnIndex(DbHelper.AMOUNT))+" )";
                noteamt = "Let's try this";
                if(m==n)
                {
                    curr.add(noteamt);
                    totcurr+=amt;
                }
                else if(m==n-1)
                {
                    currm1.add(noteamt);
                    totcurrm1+=amt;
                }
                else if(m==n-2)
                {
                    currm2.add(noteamt);
                    totcurrm2+=amt;
                }
            }
        }
        c.close();



        String m1 = goswitch(n);
        months_parent.put(m1+" ( "+totcurr+" )", curr); //exception here
        String m2 = goswitch(n-1);
        months_parent.put(m2+" ( "+totcurrm1+" )",currm1);//exception here
        String m3 = goswitch(n-2);
        months_parent.put(m3+" ( "+totcurrm2+" )", currm2); //exception here

        child_list = new ArrayList<String>(months_parent.keySet());
        adapter = new monthAdapter(getActivity(), months_parent, child_list);
        exp_list.setAdapter(adapter);
        return v;
    }

    String goswitch(int n)
    {
        String monthyr = null;
        switch(n)
        {
        case 0:
            return "January "+y;
        case 1:
            return "February "+y;
        case 2:
            return "March "+y;
        case 3:
            return "April "+y;
        case 4:
            return "May "+y;
        case 5:
            return "June "+y;
        case 6:
            return "July "+y;
        case 7:
            return "August "+y;
        case 8:
            return "September "+y;
        case 9:
            return "October "+y;
        case 10:
            return "November "+y;
        case 11:
            return "December "+y;
        }
        return monthyr;
    }
}

使用 put 的三行是我得到错误的地方。这是 LogCat:

11-09 21:11:31.610:E/AndroidRuntime(2683):致命异常:主要 11-09 21:11:31.610: E/AndroidRuntime(2683): java.lang.NullPointerException 11-09 21:11:31.610: E/AndroidRuntime(2683):在 com.twistedlines.overbudget.HistoryFragment.onCreateView(HistoryFragment.java:104) 11-09 21:11:31.610: E/AndroidRuntime(2683): 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) 11-09 21:11:31.610: E/AndroidRuntime(2683): 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 11-09 21:11:31.610: E/AndroidRuntime(2683): 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 11-09 21:11:31.610: E/AndroidRuntime(2683): 在 android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 11-09 21:11:31.610: E/AndroidRuntime(2683): 在 android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 11-09 21:11:31.610: E/AndroidRuntime(2683): 在 android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) 11-09 21:11:31.610: E/AndroidRuntime(2683): 在 android.os.Handler.handleCallback(Handler.java:615) 11-09 21:11:31.610:E/AndroidRuntime(2683):在 android.os.Handler.dispatchMessage(Handler.java:92) 11-09 21:11:31.610:E/AndroidRuntime(2683):在 android.os.Looper.loop(Looper.java:137) 11-09 21:11:31.610: E/AndroidRuntime(2683):在 android.app.ActivityThread.main(ActivityThread.java:4792) 11-09 21:11:31.610:E/AndroidRuntime(2683):在 java.lang.reflect.Method.invokeNative(Native Method)

谁能告诉我怎么了?

【问题讨论】:

    标签: android nullpointerexception expandablelistview


    【解决方案1】:

    您似乎正试图将值放入可为空的哈希图中 你忘了初始化months_parent,所以在oncreateview方法中初始化为

      months_parent = new HashMap<String,List<String>>();
    

    或改变

    HashMap<String,List<String>> months_parent;
    

    HashMap<String,List<String>> months_parent = new HashMap<String,List<String>>();
    

    【讨论】:

    • 非常感谢。愚蠢的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 2013-07-19
    • 2012-04-20
    • 2013-07-10
    • 2019-04-15
    • 2011-07-28
    相关资源
    最近更新 更多