单击列表项后显示对话框

时间:2013-12-29 02:17:07

标签: android listview onitemclicklistener android-adapterview

列表正常工作。点击后想显示自己的对话,但是错误:

  

android.view.WindowManager $ BadTokenException:无法添加窗口 -   token null不适用于应用程序

我的代码:

public class View_List_Of_Reports extends Activity{
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_list_of_reports);

        String[] nazwyRaportow = {"Podsumowanie dnia", "Sprzedaz wg marka/model", "Sprzedaz wg marka", "Sprzedaz wg rodzaj paliwa", "Sprzedaz wg typ nadwozia"};
        String[] szczegolyRaportow = {"Podsumowanie sprzedazy w obecnymm dniu - Lista sprzedazy ","Sczegoly raportu_b ","Sczegoly raportu_c ","Sczegoly raportu_d ","Sczegoly raportu_e "};

        context = getApplicationContext();
        MobileArrayAdapter adapter = new MobileArrayAdapter(context , nazwyRaportow, szczegolyRaportow);

        ListView lista = (ListView) findViewById(R.id.listViewViewListOfReports);
        lista.setAdapter(adapter);
        lista.setOnItemClickListener(onListClick);

    }// end create

private class MobileArrayAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final String[] values;
        private final String[] values2;

        public MobileArrayAdapter(Context context, String[] values, String[] values2) {
            super(context, R.layout.row_report, values);
            this.context = context;
            this.values = values;
            this.values2 = values2;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(R.layout.row_report, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.tvRRNazwaRaportu);
            TextView textView2 = (TextView) rowView.findViewById(R.id.tvRRSczegolyRaportu);

            textView.setText(values[position]);
            textView2.setText(values2[position]);

            return rowView;
        }
    }   

private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {

                    Dialog dialog1 = new Dialog(context);
                    dialog1.setContentView(R.layout.view_dialog1);
                    dialog1.show();

    }

};
}// end activity

请帮忙

1 个答案:

答案 0 :(得分:4)

对于Dialog,您必须使用Activity's instance而不是application context

试试这个

private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {

                    Dialog dialog1 = new Dialog(View_List_Of_Reports.this);
                    dialog1.setContentView(R.layout.view_dialog1);
                    dialog1.show();

    }

};