我有一个自定义 SimpleCursor listView 和 Chronometer。我尝试在Chronometer
(timer) 为 00:10 时绘制一行颜色,但它会抛出 NullPointerException。
感谢您的帮助这是代码:
public class ListViewcursorActivity extends ListActivity {
/** Called when the activity is first created. */
Chronometer timer;
ListView lvItems;
SimpleCursorAdapter colours;
private testDBAdapter thisTestDBAdapter;
LinearLayout[] Row;
View[] v;
@Override
public void onCreate(Bundle savedInstanceState) {
thisTestDBAdapter = new testDBAdapter(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvItems =getListView();fillData();
timer = (Chronometer)findViewById(R.id.chronometer1);
timer.setBase(android.os.SystemClock.elapsedRealtime());
timer.start();
Row =new LinearLayout[lvItems.getCount()];
v = new View[lvItems.getCount()];
for(int i=0;i<lvItems.getChildCount();i++) {
v[i] =lvItems.getChildAt(i);
Row[i] = (LinearLayout)v[i].getParent();
}
timer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
public void onChronometerTick(Chronometer chronometer) {
// TODO Auto-generated method stub
for(int i=0;i<lvItems.getChildCount();i++){
// TextView child =(TextView)Row[i].getChildAt(0);
if( chronometer.getText().toString().equalsIgnoreCase("Timer: 00:10")){
try {
Row[1].setBackgroundColor(Color.RED);
} catch (NullPointerException e) {
// TODO Auto-generated catch block
Toast.makeText(ListViewcursorActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
}
});
}
问题是: // TextView child =(TextView)Row[i].getChildAt(0); 和 Row[1].setBackgroundColor(Color.RED);
06-02 17:00:57.263: E/AndroidRuntime(869): Uncaught handler: thread main exiting due to uncaught exception
06-02 17:00:57.263: E/AndroidRuntime(869): java.lang.NullPointerException
06-02 17:00:57.263: E/AndroidRuntime(869): at net.deneme.cursorlist.ListViewcursorActivity$1.onChronometerTick(ListViewcursorActivity.java:91)
06-02 17:00:57.263: E/AndroidRuntime(869): at android.widget.Chronometer.dispatchChronometerTick(Chronometer.java:276)
06-02 17:00:57.263: E/AndroidRuntime(869): at android.widget.Chronometer$1.handleMessage(Chronometer.java:268)
06-02 17:00:57.263: E/AndroidRuntime(869): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 17:00:57.263: E/AndroidRuntime(869): at android.os.Looper.loop(Looper.java:123)
06-02 17:00:57.263: E/AndroidRuntime(869): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-02 17:00:57.263: E/AndroidRuntime(869): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 17:00:57.263: E/AndroidRuntime(869): at java.lang.reflect.Method.invoke(Method.java:521)
06-02 17:00:57.263: E/AndroidRuntime(869): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-02 17:00:57.263: E/AndroidRuntime(869): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-02 17:00:57.263: E/AndroidRuntime(869): at dalvik.system.NativeStart.main(Native Method)
06-02 17:00:57.273: I/dalvikvm(869): threadid=7: reacting to signal 3
06-02 17:00:57.273: E/dalvikvm(869): Unable to open stack trace file '/data/anr/traces.txt': Permission denied