从Adapter + ListView打开时出现对话框错误

时间:2014-02-26 02:21:05

标签: android listview android-listview dialog adapter

我对Dialog有点问题。 这是一个带有缩略图的视频列表视图,可以使用适配器加载视频。 ListView注册一个OnItemClickListener,在OnClickItem方法中我尝试引发Dialog。

我尝试过各种类型的Dialog但没有发生任何事情。这是一段简化的代码:

public class ListOfVideos extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.list_of_videos);
          init_phone_video_grid();
    }

    private void init_phone_video_grid() {

        // Here's some code for the video reading

        // The ListView
        videolist = (ListView) findViewById(R.id.PhoneVideoList);
        videolist.setAdapter(new VideoAdapter(getApplicationContext()));
        videolist.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                // Here's some code for the video reading

                /** ============= Here's the problem ================ **/
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setMessage("Example Message")
                        .setTitle("This is the title!!")
                        .setCancelable(false)
                        .setNeutralButton("Ok",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });
                AlertDialog alert = builder.create();
                alert.show();

                System.out.println("[debug]" + "Open File " + filename);
            }
        });
    }

视频列表加载完美。但是当我点击一个项目时:

  1. 对话框未显示

  2. 我在LogCat中收到一条错误消息,说明:“show()对话框的窗口为空!”

  3. println调试消息在LogCat

  4. 中显示正常

    我搜索过该邮件错误,但信息不多。

    我认为问题可能在接收构建器的Context上,但我坚持这一点。

    任何建议都是apreciated

2 个答案:

答案 0 :(得分:1)

以下是如何创建对话框的示例..

        String message = "Hello";
        AlertDialog.Builder alt_bld = new AlertDialog.Builder(
                CurrentActi.this);
        alt_bld.setTitle("Alert")
        .setMessage(message)
        .setCancelable(false)
        .setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

                //here right the code that you want perform onClick

                dialog.cancel();
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alert = alt_bld.create();
        alert.setTitle("Alert");
        alert.show();

可能会帮助你..

答案 1 :(得分:1)

您应该从Context获取AlertDialog.Builder Activity,而 View传入方法

改变这个:

AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

到此:

AlertDialog.Builder builder = new AlertDialog.Builder(ListOfVideos.this);
相关问题