Android 应用程序在使用AlertDialogs时崩溃

Android 应用程序在使用AlertDialogs时崩溃,android,android-alertdialog,Android,Android Alertdialog,我已经设置了一个列表,当我触摸列表中的一项时,我希望应用程序生成一个AlertDialog。然而,当我触摸一个项目时,应用程序崩溃,我现在知道原因了。这是我的密码 DATA = new ArrayList<Student>(); adapter1 = new StudentAdapter(this, R.layout.studentitemlayout, DATA); listview01 = (ListView) findViewById(R.id.ListView

我已经设置了一个列表,当我触摸列表中的一项时,我希望应用程序生成一个AlertDialog。然而,当我触摸一个项目时,应用程序崩溃,我现在知道原因了。这是我的密码

DATA = new ArrayList<Student>();
    adapter1 = new StudentAdapter(this, R.layout.studentitemlayout, DATA);
    listview01 = (ListView) findViewById(R.id.ListView01);
    listview01.setAdapter(adapter1);

    listview01.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i(DEBUG_TAG, "You clicked " + position + " student");
            AlertDialogFragment fragment = new AlertDialogFragment();
            fragment.onCreateDialog(savedInstanceState).show();
        }
    });
}

还有最后一个问题 为什么这段代码会失败并导致应用程序崩溃

/*newLessonAlertDialog fragment = new newLessonAlertDialog();
            fragment.show(getFragmentManager(),DEBUG_TAG); */
            AlertDialog alert = new AlertDialog.Builder(getApplicationContext()).create();
            alert.setTitle("nikos");
            alert.show();
称之为秀。不需要调用onCreateDialog


您使用的是DialogFragment,因此应该调用fragment.show而不是onCreateDialog方法。

谢谢,这对我很有效,但我不明白为什么。当我生成一个片段时,如何调用onCreateDialog?它是在片段中完成的。阅读文档@user3596026接受答案怎么样?它帮了你,莱特?
/*newLessonAlertDialog fragment = new newLessonAlertDialog();
            fragment.show(getFragmentManager(),DEBUG_TAG); */
            AlertDialog alert = new AlertDialog.Builder(getApplicationContext()).create();
            alert.setTitle("nikos");
            alert.show();
fragment.show(getFragmentManager(), "Some tag");