创建handler对象指定一个Callback的代码报错

public class MainActivity extends Activity implements OnClickListener{
private TextView textView;
private ImageView imageView;
private int index;
private int images [] = {R.drawable.image1,R.drawable.image2,R.drawable.image3};
private MyRunnable runnable = new MyRunnable();
private Button button;

class Person{
    public int age;
    public String name;

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "name=" + name + " age=" + age;
    }
}

private Handler handler = new Handler(new Callback() {

    @Override
    public boolean handleMessage(Message msg) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(),"" + 1, 1).show();
        return false;
    }
}){
    public void handleMessage(Message msg){
        Toast.makeText(getApplicationContext(),"" + 2, 1).show();
    }       
};

class MyRunnable implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        index++;
        index = index % 3;
        imageView.setImageResource(images[index]);
        handler.postDelayed(runnable, 1000);
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView = (ImageView) findViewById(R.id.imageView1);
    textView = (TextView) findViewById(R.id.textview);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(this);

    new Thread(){
        public void run() {
            try {
                Thread.sleep(2000);
                Message message = handler.obtainMessage();                  
                Person person = new Person();
                person.age = 12;
                person.name = "mike";
                message.obj = person;
                message.sendToTarget();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        };
    }.start();

    handler.postDelayed(runnable, 1000);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    handler.sendEmptyMessage(1);
}

}

按照学习视频打得,一模一样,还是报错。
private Handler handler = new Handler(new Callback() {

    @Override
    public boolean handleMessage(Message msg) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(),"" + 1, 1).show();
        return false;
    }
}){
    public void handleMessage(Message msg){
        Toast.makeText(getApplicationContext(),"" + 2, 1).show();
    }       
};

搞不懂
problems报错信息:大概意思是handler没有这种定义的构造,第二个是handleMessage方法需要重写或实现。首先这个方法是视频上面大的,我是照葫芦画瓢。第二这个方法已经重写了,有Toast...
Description Resource    Path    Location    Type

The constructor Handler(new Callback(){}) is undefined MainActivity.java /handler_01/src/com/hao123/handler_01 line 35 Java Problem
Description Resource Path Location Type
The method handleMessage(Message) of type new Callback(){} must override or implement a supertype method MainActivity.java /handler_01/src/com/hao123/handler_01 line 38 Java Problem
我试过如果改一下标点符号就报个标点符号的错误 ,例如把最后一排括回来的大括号和分号删除
Description Resource Path Location Type
Syntax error, insert ";" to complete FieldDeclaration MainActivity.java /handler_01/src/com/hao123/handler_01 line 121 Java Problem
Description Resource Path Location Type
Description Resource Path Location Type
Syntax error, insert "}" to complete ClassBody MainActivity.java /handler_01/src/com/hao123/handler_01 line 121 Java Problem
但是这种报错是那种坑爹的报错,明显那个地方是要标点的

查看全部
qq_29754803
qq_29754803
2015/09/13 09:08
  • handlemessage
  • android
  • callback
  • 点赞
  • 收藏
  • 回答
    私信

2个回复