android activity 实现 OnClickListener 接口,触发报异常!!!!!!!!
publicclassBundleActivityextendsActivityimplementsOnClickListener{/**Calledwhentheact...
public class BundleActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button =(Button) findViewById(R.id.myButton);
button.setOnClickListener(new BundleActivity());
}
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("name", "wezhu");
bundle.putString("age","123");
intent.putExtras(bundle);
intent.setClass(BundleActivity.this, ForwardActivity.class);
startActivity(intent);
}
}
button.setOnClickListener(new BundleActivity());
为什么这行代码不能用?? 展开
/** Called when the activity is first created. */
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button =(Button) findViewById(R.id.myButton);
button.setOnClickListener(new BundleActivity());
}
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("name", "wezhu");
bundle.putString("age","123");
intent.putExtras(bundle);
intent.setClass(BundleActivity.this, ForwardActivity.class);
startActivity(intent);
}
}
button.setOnClickListener(new BundleActivity());
为什么这行代码不能用?? 展开
2个回答
展开全部
button.setOnClickListener(new BundleActivity());
应该改为:button.setOnClickListener(this);意思是监听现在activity的按钮。
New了一个activity,这个activity也没显示,监听它的按钮没有意义。
还有就是implements OnClickListener:implements是不可以覆盖父类里的函数,在public void onClick(View v)前加一个@Override,就Ok了
应该改为:button.setOnClickListener(this);意思是监听现在activity的按钮。
New了一个activity,这个activity也没显示,监听它的按钮没有意义。
还有就是implements OnClickListener:implements是不可以覆盖父类里的函数,在public void onClick(View v)前加一个@Override,就Ok了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
拜托,你这完全写错了啊!
应该这样写!
public class BundleActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button =(Button) findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("name", "wezhu");
bundle.putString("age","123");
intent.putExtras(bundle);
intent.setClass(BundleActivity.this, ForwardActivity.class);
startActivity(intent);
}
});
}
}
应该这样写!
public class BundleActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button =(Button) findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("name", "wezhu");
bundle.putString("age","123");
intent.putExtras(bundle);
intent.setClass(BundleActivity.this, ForwardActivity.class);
startActivity(intent);
}
});
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询