2014-02-26 68 views
1

我有一个对话框有点问题。 这是一个带有缩略图的视频列表视图,可以通过适配器加载视频。 ListView注册一个OnItemClickListener,并在OnClickItem方法中尝试引发对话框。当从适配器打开时出现对话框错误+ ListView

我已经尝试过各种类型的对话框,但没有发生任何事情。这是一个简化的代码片段:

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的一个错误信息,这种状态:“!)节目(Dialog的窗口为空”

  3. println的调试消息,出现在logcat的

...我已经寻找该消息错误不错,但没有太多的信息。

我认为问题可能出现在接收Builder的上下文中,但我坚持这一点。

任何意见将apreciated

+3

尝试获得从活动的,而不是通过查看AlertDialog.Builder语境。即,ListOfVideos.this而不是v.getContext()。 –

+0

地狱呀!那就是诀窍......如果你把它作为答复发布,我会接受它...... :) 非常感谢你 –

+0

酷!谢啦! –

回答

1

你应该得到Context你重新创建AlertDialog.BuilderActivity而不是View传入该方法。

更改此:

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

这样:

AlertDialog.Builder builder = new AlertDialog.Builder(ListOfVideos.this); 
+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(); 

可能它会帮助你..

相关问题