作者:涅槃WB | 来源:互联网 | 2023-09-01 11:26
publicclassContactActivityextendsActivity{ImageButtonbtn_img;AlertDialogimageChooseDialog
public class ContactActivity extends Activity {
ImageButton btn_img;
AlertDialog imageChooseDialog;//点击图像出现对话框
private int[] images = {R.drawable.img1,R.drawable.img2};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnew);
btn_img = (ImageButton)super.findViewById(R.id.btn_img);
btn_img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
initImageChooseDialog();
imageChooseDialog.show();
}});
}
private void initImageChooseDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择图像");
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.imageswitch, null);
imageChooseDialog = builder.create();
}
View view = inflater.inflate(R.layout.imageswitch, null);
运行时 多了这句就老是出错 求大神帮忙 在线等
8 个解决方案
解决了,原来上条语句结尾的“;”不小心打成“。”了
LayoutInflater inflater = LayoutInflater.from(ContactActivity .this);
试试这样替换一下,
你的 initImageChooseDialog(); 方法是在 public void onClick(View v) 中调用的,所以this不是当前Activity的上下文环境,需要用 ContactActivity .this 代替。