一个监听器控制两个button
点击两个button,弹出对话框
错误是Android.widget.TextView cannot be cast to Android.widget.Button
实在找不出哪里错啊
请教各位
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button pickDate;
private Button zhuce;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pickDate=(Button)findViewById(R.id.pickeDate);
zhuce=(Button)findViewById(R.id.zhuce);
//OnClickListenerImp1 listener=new OnClickListenerImp1();
pickDate.setOnClickListener(new OnClickListenerImp1());
zhuce.setOnClickListener(new OnClickListenerImp1());//设置事件
}
class OnClickListenerImp1 implements OnClickListener{
@Override
public void onClick(View view) {
if(R.id.pickeDate==view.getId()){
Dialog dialogDate=new AlertDialog.Builder(MainActivity.this)
.setTitle("请选择日期")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.create();
dialogDate.show();
}
if(R.id.zhuce==view.getId()){
Dialog dialogZhuce=new AlertDialog.Builder(MainActivity.this)
.setTitle("请选择日期")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.create();
dialogZhuce.show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
------解决方案--------------------提示出错行在什么地方?
------解决方案--------------------pickDate=(Button)findViewById(R.id.pickeDate);
zhuce=(Button)findViewById(R.id.zhuce);
pickeDate、zhuce这两个里有一个是TextView,不是Button。
目测pickeDate是TextView。
------解决方案-------------------- dialogZhuce.show();错误,应该改为dialogDate.show();,这是因为dialogDate才是AlertDialog对象