试图重写getView在SimpleCursorAdapter给NullPointerExceptio [英] Trying to override getView in a SimpleCursorAdapter gives NullPointerExceptio

查看:119
本文介绍了试图重写getView在SimpleCursorAdapter给NullPointerExceptio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将非常AP preciate任何帮助或暗示的是下一步要做什么。

我想以编程方式更改行的ListView中的内容。在一排有3个TextView的和进度。我想动画进度,如果当前行中的结果一栏为零。

阅读一些教程和文档之后,我来到了LayoutInflater必须使用与 getView()的结论 - 覆盖。也许我错了这一点。

如果我回到行= inflater.inflate(R.layout.row,NULL); 从功能,它提供了NullPointerException异常

下面是code:

 私人最后一类mySimpleCursorAdapter扩展SimpleCursorAdapter {

    私人光标localCursor;
    私人语境localContext;

    公共mySimpleCursorAdapter(上下文的背景下,INT布局,光标C,的String []从,INT []键){
        超(背景下,布局,C,从,到);
        this.localCursor = C;
        this.localContext =背景;

    }

    / **
     * 1的ListView要求适配器给我一个视图(getView)的列表中的每个项目
     * 2。新的视图返回并显示
     * /
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看排= super.getView(位置,convertView,父母);

        LayoutInflater充气=(LayoutInflater)localContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        字符串结果= localCursor.getString(2);
        INT resInt =的Integer.parseInt(结果);

        Log.d(TAG,行+行);

        //如果结果列构成表是0,做一些有用的东西:
        如果(resInt == 0){
            进度进度=(进度)row.findViewById(R.id.update_progress);
            progress.setIndeterminate(真正的);

            TextView的EDIT1 =(TextView中)row.findViewById(R.id.row_id);
            TextView的EDIT2 =(TextView中)row.findViewById(R.id.request);
            TextView的EDIT3 =(TextView中)row.findViewById(R.id.result);
            edit1.setText(1);
            edit2.setText(2);
            edit3.setText(3);
            行= inflater.inflate(R.layout.row,NULL);
        }

        返回行;
    }
 

这里是堆栈跟踪:

 三月三号至八号:15:29.639:ERROR / AndroidRuntime(619):显示java.lang.NullPointerException
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:149)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.CursorAdapter.getView(CursorAdapter.java:186)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在com.dhristov.test1.test1 $ mySimpleCursorAdapter.getView(test1.java:105)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.AbsListView.obtainView(AbsListView.java:1256)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.ListView.makeAndAddView(ListView.java:1668)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.ListView.fillDown(ListView.java:637)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.ListView.fillSpecific(ListView.java:1224)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.ListView.layoutChildren(ListView.java:1499)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.AbsListView.onLayout(AbsListView.java:1113)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.view.View.layout(View.java:6830)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.LinearLayout.onLayout(LinearLayout.java:918)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.view.View.layout(View.java:6830)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.LinearLayout.onLayout(LinearLayout.java:918)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.view.View.layout(View.java:6830)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.view.View.layout(View.java:6830)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.LinearLayout.onLayout(LinearLayout.java:918)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.view.View.layout(View.java:6830)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.view.View.layout(View.java:6830)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.view.ViewRoot.performTraversals(ViewRoot.java:996)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.os.Handler.dispatchMessage(Handler.java:99)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.os.Looper.loop(Looper.java:123)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在android.app.ActivityThread.main(ActivityThread.java:4363)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在java.lang.reflect.Method.invokeNative(本机方法)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在java.lang.reflect.Method.invoke(Method.java:521)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
3月三号至8日:15:29.639:ERROR / AndroidRuntime(619):在dalvik.system.NativeStart.main(本机方法)
 

解决方案

有关的CursorAdapter 和子类,你应该重写 NewView的() bindView()而不是 getView()

更重要的是,你不应该得到调用 super.getView()。这就是你的崩溃。

Would very much appreciate any help or hint on were to go next.

I'm trying to change the content of a row in ListView programmatically. In one row there are 3 TextView and a ProgressBar. I want to animate the ProgressBar if the 'result' column of the current row is zero.

After reading some tutorials and docs, I came to the conclusion that LayoutInflater has to be used and getView() - overriden. Maybe I am wrong on this.

If I return row = inflater.inflate(R.layout.row, null); from the function, it gives NullPointerException.

Here is the code:

    private final class mySimpleCursorAdapter extends SimpleCursorAdapter {

    private Cursor localCursor;
    private Context localContext;

    public mySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);            
        this.localCursor = c;
        this.localContext = context;

    }

    /**
     *    1. ListView asks adapter "give me a view" (getView) for each item of the list
     *    2. A new View is returned and displayed
     */
    public View getView(int position, View  convertView, ViewGroup  parent) {
        View row = super.getView(position, convertView, parent);

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

        String result = localCursor.getString(2);
        int resInt = Integer.parseInt(result);

        Log.d(TAG, "row " + row);

        // if 'result' column form the TABLE is 0, do something useful:
        if(resInt == 0) {           
            ProgressBar progress = (ProgressBar) row.findViewById(R.id.update_progress);
            progress.setIndeterminate(true);

            TextView edit1 = (TextView)row.findViewById(R.id.row_id);
            TextView edit2 = (TextView)row.findViewById(R.id.request);
            TextView edit3 = (TextView)row.findViewById(R.id.result);
            edit1.setText("1");
            edit2.setText("2");
            edit3.setText("3");
            row = inflater.inflate(R.layout.row, null);             
        }

        return row;
    }

here is the Stack Trace:

03-08 03:15:29.639: ERROR/AndroidRuntime(619): java.lang.NullPointerException
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:149)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at com.dhristov.test1.test1$mySimpleCursorAdapter.getView(test1.java:105)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.AbsListView.obtainView(AbsListView.java:1256)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.ListView.makeAndAddView(ListView.java:1668)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.ListView.fillDown(ListView.java:637)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.ListView.fillSpecific(ListView.java:1224)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.ListView.layoutChildren(ListView.java:1499)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.AbsListView.onLayout(AbsListView.java:1113)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.os.Looper.loop(Looper.java:123)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at java.lang.reflect.Method.invoke(Method.java:521)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-08 03:15:29.639: ERROR/AndroidRuntime(619):     at dalvik.system.NativeStart.main(Native Method)

解决方案

For CursorAdapter and subclasses, you should override newView() and bindView() instead of getView().

More importantly, though, you should not get calling super.getView(). That is where you are crashing.

这篇关于试图重写getView在SimpleCursorAdapter给NullPointerExceptio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆