2012-01-10 24 views
3
public void onListItemClick(ListView parent, View v, int position, long id) 
    {   
     super.onListItemClick(parent, v, position, id); 



    LayoutInflater inflater = (LayoutInflater) 
     this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
final PopupWindow pw = new PopupWindow(
     inflater.inflate(R.layout.offer_popup, null, false), 
     500, 
     600, 
     true); 

pw.showAtLocation(this.findViewById(android.R.id.list), Gravity.CENTER, 0, 0);  

ImageView closeimage=(ImageView) findViewById(R.id.imageView2); 
closeimage.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    pw.dismiss(); 
    } 
}); 

offer_popup.xml辭退彈出當點擊一個圖像

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:theme="@android:style/Theme.Dialog" 
android:orientation="vertical" android:weightSum="1"> 
<RelativeLayout android:layout_width="fill_parent" 
android:id="@+id/relativeLayout_popup" 
    android:layout_height="wrap_content" android:layout_weight="0.65"> 
    <ImageView android:layout_width="wrap_content" 
    android:src="@drawable/node_largeview_black" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView1" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
     android:layout_marginTop="61dp"></ImageView> 
    <ImageView android:id="@+id/imageView2" 
    android:layout_height="wrap_content" 
    android:src="@drawable/close_button" 
    android:layout_width="wrap_content" 
    android:layout_alignTop="@+id/imageView1" 
    android:layout_alignRight="@+id/imageView1"></ImageView> 
    </RelativeLayout> 
</LinearLayout> 

我有一個列表視圖,如果我點擊條款行之一,彈出創建將使用offer_popup.xml,這裏面XML,有一個imageView2 ID是一個密切的圖像,並點擊它,彈出框將被解散。

但是,程序崩潰closeimage = null。

回答

1
View popView = inflater.inflate(R.layout.offer_popup, null, false); 
final PopupWindow pw = new PopupWindow(popView 
     , 
     500, 
     600, 
     true); 
ImageView closeimage=(ImageView) popView.findViewById(R.id.imageView2); 

改變你的代碼,上面的,你需要給該圖像的家長參考找到它,如果它不是主要佈局的一部分。

+0

好了,現在也沒有崩潰,但是,我點擊closeimage,在彈出的圖像沒有得到關閉。 – lilzz 2012-01-10 16:44:35

1

offer_popup XML的View文件,並使用該視圖中找到的ImageView ..

View popViewWindow = inflater.inflate(R.layout.offer_popup, null, false); 
ImageView closeimage=(ImageView)popViewWindow.findViewById(R.id.imageView2); 
+0

好吧,現在它沒有崩潰,但我點擊closeimage,彈出窗口的圖像沒有關閉。 – lilzz 2012-01-10 16:45:00

1

我不認爲你的closeimage是您的活動的視圖層次的一部分。請嘗試保存到offer_popup視圖的引用,並從那裏得到closeimage:

View popupView = inflater.inflate(R.layout.offer_popup, null, false); 
final PopupWindow pw = new PopupWindow(
    popupView, 
    500, 
    600, 
    true); 
ImageView closeimage=(ImageView) popupView.findViewById(R.id.imageView2); 
//... 
+0

好,現在它沒有崩潰,但我點擊closeimage,彈出窗口的圖像沒有關閉。 – lilzz 2012-01-10 16:44:48

+0

我想嘗試在調試器中放入一些日誌消息或者跟蹤,看看你的onclick監聽器是否被調用。如果不是,請嘗試更改closeimage的src,並確保它是您認爲的那個,並且您正在點擊正確的位置。檢查pw是否指向彈出窗口,而不是null或意外。試圖關閉彈出窗口時觀察logcat,並查看是否有任何異常被拋出。這些只是想到的第一個想法 - 你的猜測可能和我一樣好。祝你好運! – dokkaebi 2012-01-12 18:37:27

相關問題