Android handler的报错

使用handler 更新应用

 }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.messages, menu);
        return true;
    }

    class UpdateTask implements Runnable {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            setContentView(R.layout.activity_messages);

            Intent intent = getIntent();

            String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
            String id = intent.getStringExtra(MainActivity.EXTRA_ID);
            String[] lst = null;
            ListView lm=(ListView)findViewById(R.id.listView1);
            TextView tv = (TextView)findViewById(R.id.textView1);

            tv.setText("Welcome " + message);

            CallSoap cs=new CallSoap();

            lst=cs.GetMessage(id);

            ArrayAdapter<String> adpt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lst);

            lm.setAdapter(adpt);

            handler.postDelayed(this, 500);
        }
    }
}

但是报错:

ArrayAdapter<String> adpt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lst);

Text: Constructor ArrayAdapter is undefined

ArrayAdapter adpt = new ArrayAdapter(this, android.R.layout.simple_list_item_1,lst); 构造函数的第一个参数是context,所以应该写成XXXActivity.this 。不过楼主在 runnable即非Ui线程里处理界面是会出问题的

你这个this代表的是这个Runnable 应该写成YourActivity.this

这里的this的问题,this不是指代之前那个activity了!你可以把它传递过来