2011-12-21 161 views
0

我:的Android:彈出窗口和按鈕關閉不工作

popUp.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 

      .... 
    <Button 
     android:id="@+id/ok_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ok" 
     android:gravity="left" 
     /> 
</LinearLayout> 

main.java

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ListView listView = (ListView)findViewById(R.id.list); 
    listView.setOnItemClickListener(new OnItemClickListener() 
       { 
       public void onItemClick(AdapterView<?> parent, View v, 
       int position, long id) 
       { 
        popUp();  

       } 
     }); 

,我得到強制關閉這裏

public void popUp(){ 
      LayoutInflater inflater = (LayoutInflater) project.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      final PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup,null, false),300,400,true); 
      ok_button = (Button) findViewById(R.id.ok_button); 
      pw.showAtLocation(findViewById(R.id.list), Gravity.BOTTOM, 0,10); 
      ok_button.setOnClickListener(new OnClickListener() { //here I get FC 
       @Override 
       public void onClick(View v) { 

       pw.dismiss(); 

       } 
    }); 
     } 


    } 

當我使用按鈕(在popUp();)從main.x毫升而不是popUp.xml一切正常。 什麼是錯的使用按鈕從沒有main.xml中

回答

2

我覺得現在的問題是在這裏:

ok_button = (Button) findViewById(R.id.ok_button); 

它正在尋找主要佈局內的按鈕來查看,而不是尋找彈出佈局內。所以可能ok_button是空的。

嘗試將充氣呼叫從構造函數中搬出來,像這樣:

View v = inflater.inflate(R.layout.popup,null, false); 
final PopupWindow pw = new PopupWindow(v,300,400,true); 

然後:

ok_button = (Button) v.findViewById(R.id.ok_button); 
+0

它的工作。謝謝。 – aptyp 2011-12-21 20:09:36