我有一个自定义AlertDialog
,它的布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="200dp"
android:background="#ffd200"
android:gravity=""
android:orientation="vertical" >
<Button
android:id="@+id/btn_Open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open" />
<Button
android:id="@+id/btn_Copy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Copy" />
<Button
android:id="@+id/btn_Cut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cut" />
<Button
android:id="@+id/btn_Back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Back" />
</LinearLayout>
和java代码是这样的:
public class MainActivity extends Activity {
private ListView lstName;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstName = (ListView)findViewById(R.id.lstNames);
String[] names = {"House","car","computer"};
//Toast.makeText(getApplicationContext(), names[names.length-1].toString(), Toast.LENGTH_LONG).show();
//lstName.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,names));
lstName.setAdapter(new ArrayAdapter<String>(this, R.layout.row,R.id.textView1,names));
final AlertDialog.Builder bb = new AlertDialog.Builder(this);
LayoutInflater inf= this.getLayoutInflater();
View menu = inf.inflate(R.layout.menu2, null);
bb.setView(menu);
lstName.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "it's " + lstName.getItemAtPosition(arg2).toString() , Toast.LENGTH_LONG).show();
AlertDialog myalert2 = bb.create();
myalert2.show();
}
});
}
}
我在模拟器中运行后,alertdialog
可以点击项目,但是用back
键取消它或触摸暗区,不起作用,程序将被强制关闭...
你能指导我吗?
logCat是这样的:
12-13 23:49:26.583: D/AndroidRuntime(6524): Shutting down VM
12-13 23:49:26.583: W/dalvikvm(6524): threadid=1: thread exiting with uncaught exception (group=0xa000f180)
12-13 23:49:26.653: E/AndroidRuntime(6524): FATAL EXCEPTION: main
12-13 23:49:26.653: E/AndroidRuntime(6524): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.view.ViewGroup.addViewInner(ViewGroup.java:3337)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.view.ViewGroup.addView(ViewGroup.java:3208)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.view.ViewGroup.addView(ViewGroup.java:3188)
12-13 23:49:26.653: E/AndroidRuntime(6524): at com.android.internal.app.AlertController.setupView(AlertController.java:401)
12-13 23:49:26.653: E/AndroidRuntime(6524): at com.android.internal.app.AlertController.installContent(AlertController.java:241)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.app.AlertDialog.onCreate(AlertDialog.java:336)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.app.Dialog.dispatchOnCreate(Dialog.java:353)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.app.Dialog.show(Dialog.java:257)
12-13 23:49:26.653: E/AndroidRuntime(6524): at com.example.mytest31212444.MainActivity$1.onItemClick(MainActivity.java:49)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.widget.AbsListView$1.run(AbsListView.java:3168)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.os.Handler.handleCallback(Handler.java:605)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.os.Looper.loop(Looper.java:137)
12-13 23:49:26.653: E/AndroidRuntime(6524): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-13 23:49:26.653: E/AndroidRuntime(6524): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 23:49:26.653: E/AndroidRuntime(6524): at java.lang.reflect.Method.invoke(Method.java:511)
12-13 23:49:26.653: E/AndroidRuntime(6524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-13 23:49:26.653: E/AndroidRuntime(6524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-13 23:49:26.653: E/AndroidRuntime(6524): at dalvik.system.NativeStart.main(Native Method)
12-13 23:49:27.173: I/dalvikvm(6524): threadid=3: reacting to signal 3
12-13 23:49:27.183: I/dalvikvm(6524): Wrote stack traces to '/data/anr/traces.txt'
答案 0 :(得分:0)
使用此:
lstName.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
final AlertDialog.Builder bb = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inf= MainActivity.this.getLayoutInflater();
View menu = inf.inflate(R.layout.menu2, null);
bb.setView(menu);
Toast.makeText(getApplicationContext(), "it's " + lstName.getItemAtPosition(arg2).toString() , Toast.LENGTH_LONG).show();
AlertDialog myalert2 = bb.create();
myalert2.show();
}
});
答案 1 :(得分:0)
@ρяσѕρєяK的答案对我有用......他在评论中提到了......
在AlertDialog myalert2 = bb.create();
行
onItemClick
移动bb.setView(menu);
行