我有2张Colums的桌子:GOAL(文字)和FLAG(0或1) 如果来自GOAG的值= 1来自GOAL的文本获得Strike。
final TextView txt=null;
scAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.tvText ){
if (cursor.getInt(cursor.getColumnIndex(DB.COLUMN_FLAG)) ==1 ){
String goal = cursor.getString(cursor.getColumnIndex(DB.COLUMN_GOAL));
txt.setText(goal);
txt.setPaintFlags(txt.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
}
return false;
}
});
相当简单的代码,但从String转换为Text的问题。
我看到两种方式:打击价值或修复此问题。
也许有另一种方式......
请帮助!
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): FATAL EXCEPTION: main
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): **java.lang.NullPointerException**
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at com.example.dayplan.mar2014$2.setViewValue(mar2014.java:108)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:126)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.ListView.makeAndAddView(ListView.java:1745)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.ListView.fillDown(ListView.java:670)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.ListView.fillFromTop(ListView.java:727)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.ListView.layoutChildren(ListView.java:1598)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.view.View.layout(View.java:7175)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.view.View.layout(View.java:7175)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.view.View.layout(View.java:7175)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.view.View.layout(View.java:7175)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.view.View.layout(View.java:7175)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.view.ViewRoot.performTraversals(ViewRoot.java:1142)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.os.Looper.loop(Looper.java:130)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at android.app.ActivityThread.main(ActivityThread.java:3822)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at java.lang.reflect.Method.invoke(Method.java:507)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-25 19:03:17.410: ERROR/AndroidRuntime(14935): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您需要将tvText TextView
分配给txt
final TextView txt=null;
scAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.tvText ){
//add this line
txt = (TextView)view;
if (cursor.getInt(cursor.getColumnIndex(DB.COLUMN_FLAG)) ==1 ){
String goal = cursor.getString(cursor.getColumnIndex(DB.COLUMN_GOAL));
txt.setText(goal);
txt.setPaintFlags(txt.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
else txt.setPaintFlags(txt.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
}
return false;
}
});