gpt4 book ai didi

android - 尝试覆盖 SimpleCursorAdapter 中的 getView 会给出 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:55:18 25 4
gpt4 key购买 nike

非常感谢任何关于下一步的帮助或提示。

我正在尝试以编程方式更改 ListView 中一行的内容。在一行中有 3 个 TextView 和一个 ProgressBar。如果当前行的“结果”列为零,我想为 ProgressBar 设置动画。

在阅读了一些教程和文档之后,我得出的结论是必须使用 LayoutInflater 并覆盖 getView()。也许我错了。

如果我从函数返回 row = inflater.inflate(R.layout.row, null);,它会给出 NullPointerException。

代码如下:

    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;
}

这是堆栈跟踪:

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)

最佳答案

对于 CursorAdapter 和子类,您应该覆盖 newView()bindView() 而不是 getView().

但更重要的是,您不应该调用 super.getView()。那就是你崩溃的地方。

关于android - 尝试覆盖 SimpleCursorAdapter 中的 getView 会给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2398767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com