package com.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class A1 extends Activity {
/** Called when the activity is first created. */
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new BL());
}
class BL implements OnClickListener {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("abc", "哈哈");
intent.setClass(A1.this, A2.this);
}
}
}
有些错

解决方案 »

  1.   

    package com.android;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    public class A1 extends Activity {
    /** Called when the activity is first created. */
    private Button button = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new BL());
    }
    class BL implements OnClickListener {
    public void onClick(View arg0) {
    Intent intent = new Intent();
    intent.putExtra("abc", "哈哈");
    intent.setClass(A1.this, A2.class);
    // 这时A2还不是一个对象 所以不能用A2.this 要用A2.class 属于反射机制。
    A1.this.startActivity(intent);
    // 这句话一定要有
    // 一开始我也不太明白
    // 后来查到好像是还有其他方法 所以一定要写上startActivity()方法
    // 而且A2会重新创建 如果不写 则假如A2已经被启动了 那么启动的还是原来的A2
    }
    }
    }