2012-11-18 27 views
1

我有一個自定義對話框,其格式爲Header + listview + footer。相應的代碼如下:(我不介意共享代碼,但它是800線)...自定義對話框中的listview行不可見

/* Setup dialog layout */ 
View header = (View)getLayoutInflater().inflate(R.layout.stockcountheader, null); 
View footer = (View)getLayoutInflater().inflate(R.layout.stockcountfooter, null); 

    final Dialog listDialog; 

    listDialog = new Dialog(this, android.R.style.Theme_Dialog); 
    listDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 

    LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = li.inflate(R.layout.dialog, null, false); 
    listDialog.setContentView(v); 
    listDialog.setCancelable(true); 

    final AdditemAdapter adapter = new AdditemAdapter(this, R.layout.additemrow, updateitems); 
    final ListView filterrow = (ListView) listDialog.findViewById(R.id.dialoglist); 
    filterrow.addHeaderView(header); 
    filterrow.addFooterView(footer); 
    filterrow.setAdapter(adapter); 
    i = adapter.getCount(); 
    listDialog.show(); 

在標題中,我有一個單選按鈕,並打算控制的知名度列表視圖行。所以,我有以下代碼:...

radiostockresGroup = (RadioGroup)listDialog.findViewById(R.id.radiostockres); 
    radiostockresButton = (RadioButton) radiostockresGroup.findViewById(radiostockresGroup.getCheckedRadioButtonId()); 
    radiostockresGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     public void onCheckedChanged(RadioGroup radiostockresGroup, int checkedId) { 
      radiostockresButton = (RadioButton) radiostockresGroup.findViewById(checkedId); 


      switch(checkedId) { 
       case R.id.radioresno: 

        listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);   
        break; 
       case R.id.radioresyes:      

        listDialog.findViewById(R.layout.tblayout2).setVisibility(View.VISIBLE); 
        break; 
      } 

     } 
    }); 

但是,它給我的java的空指針錯誤 「listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);」線。爲ListView和對話框中的XML如下:

stocktakerow.xml

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

    <TableLayout android:id="@+id/tblayout2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:shrinkColumns="0" 
     android:stretchColumns="*"> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content">  

      <TextView android:id="@+id/txtfieldname" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_span="3" 
       android:gravity="top" /> 

      <EditText android:id="@+id/txtinput" 
       android:layout_toRightOf="@+id/txtfieldname" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_span="3" 
       android:gravity="top" /> 


     </TableRow> 
    </TableLayout>  
</RelativeLayout> 

我嘗試使用 「filterrow.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);」並仍然有相同的空指針。

請給我一隻手,非常感謝!

+0

它是一個重複這樣的:http://stackoverflow.com/questions/10444924/setvisitility-item-in-custom-listview – kwytse

回答

0

如果你看一下你的代碼,你會發現:

final ListView filterrow = (ListView) listDialog.findViewById(R.id.dialoglist); 
listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE); 

從本質上講,你是說R.id.tblayout2是同樣的事情成員爲R.id.dialoglist。我不認爲他們在這種情況下。

沒有看到你的類延伸,我只能假設這應該工作。如果不行,你能否發佈類定義?只有像public class MainActivity extends Activity {這樣的線路可以用於兩個班級。

findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE); 
+0

感謝您的回覆。我的類是公共類CountSheet延伸活動{... listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){ \t \t \t公共無效onItemClick(適配器視圖爲arg0,ARG1查看,INT ARG2, \t \t \t \t \t長ARG3) { \t \t \t \t // TODO自動生成方法存根 \t \t stockcountdialog(爲arg0,ARG1,ARG2,ARG3,tblallcol2); \t \t \t} });我顯示的java代碼在stockcountdialog函數中。這有幫助嗎?附:如果我在xml中更改tblayout2並將代碼更改爲R.id.dialoglist,則整個對話框將不可見。 – kwytse

+0

終於,發現它是這個問題的重複:http://stackoverflow.com/questions/10444924/setvisitility-item-in-custom-listview – kwytse