1

我有一个自定义 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
4

4 回答 4

1
TextView child =(TextView) Row[i].getChildAt(0);

为什么不通过 id 获取呢?

LinearLayout row = (LinearLayout) Row[i];
if(row != null) {
    TextView textView = (TextView) row.findViewBy(R.id.textviewid);
    if(TextView != null)
       //do stuff
}

这有帮助吗?

还要更多地研究代码什么是行?线性布局对吗?这是一种不清楚的代码。也许组织得更好一些。

于 2012-06-02T17:30:25.300 回答
0

空指针异常通常意味着您正在尝试访问当前上下文中不存在的内容。

验证您的 XML 在 Java 代码中被正确引用。

于 2012-06-02T17:31:56.503 回答
0

我看不到适配器在您的onCreate. 设置适配器,看看它是否能解决问题。设置适配器后,将创建项目并且它们将具有父项。

于 2012-06-02T17:50:11.093 回答
0

对于空指针异常,您可以使用调试来跟踪空指针异常,它可以告诉您空指针异常发生的位置。

于 2012-06-02T17:51:26.950 回答