问题描述
求助各位大神,我错在哪儿了代码如下:packagecom.example.dialog;importandroid.R.drawable;importandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.DialogInterface;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Toast;publicclassMainActivityextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.button2).setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){AlertDialog.Builderbuilder=newAlertDialog.Builder(MainActivity.this);builder.setIcon(R.drawable.ic_launcher);builder.setTitle("爱好");finalString[]hobbies={"篮球","足球","网球","斯诺克"};builder.setMultiChoiceItems(hobbies,null,newDialogInterface.OnMultiChoiceClickListener(){StringBuffersb=newStringBuffer(100);@OverridepublicvoidonClick(DialogInterfacedialog,intwhich,booleanisChecked){if(isChecked){sb.append(hobbies[which]+",");}Toast.makeText(MainActivity.this,"爱好为:"+sb.toString(),Toast.LENGTH_SHORT).show();}});builder.setPositiveButton("确定",newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});builder.setNegativeButton("取消",newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});builder.show();}});}}
完全是按教程来的,没有报错,但是在模拟器中打开的话直接闪退
解决方案
解决方案二:
错误很明显的layout的xml中必须必须设置layout_width的
解决方案三:
引用1楼scqlmm的回复:
错误很明显的layout的xml中必须必须设置layout_width的
不是特别明白,我在xml中设置了layout_wdth和layout_height的
解决方案四:
检查xml的30行
解决方案五:
layout的xml中必须必须设置layout_width记得是这样