0

我有一个显示对话框的功能。

public Dialog sendSMS(){
        Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.dialogsms);
        dialog.setTitle("Send SMS");
        dialog.setCancelable(true);

        final Spinner spn = (Spinner)findViewById(R.id.spn_contatcs);
        final TextView tenso = (TextView)findViewById(R.id.txt_phone);

        final ArrayList<String> ten = new ArrayList<String>();
        final ArrayList<String> so = new ArrayList<String>();

        Cursor phones = _ketquatimkiem.this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        while (phones.moveToNext()){
                String phoneName=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                ten.add(phoneName);
                so.add(phoneNumber);
            }
        phones.close();

        ArrayAdapter<String> arrayAdapter_Contacts = new ArrayAdapter<String>(_ketquatimkiem.this,android.R.layout.simple_spinner_item,ten);
        arrayAdapter_Contacts.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spn.setAdapter(arrayAdapter_Contacts);
        spn.setOnItemSelectedListener(new OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
                tenso.setText("Phone Num:  "+so.get(arg2).toString());  
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
        return dialog;
    }

调用 onCreate()

//when i click item of listview i get quickactiondialog    
listView.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
        final QuickActionDialog quickAction = new QuickActionDialog(arg1.getContext(), QuickActionDialog.VERTICAL);   
        //when click item of quickactiondialog id == sms i show dialog 
        quickAction.setOnActionItemClickListener(new QuickActionDialog.OnActionItemClickListener() {        
            @Override
            public void onItemClick(QuickActionDialog source, int pos, int actionId) {                                      
                if(actionId == ID_SMS){
                    Dialog dialog= sendSMS();
                    dialog.show();
                }
            }
        });

但我收到错误:E/AndroidRuntime(15562): java.lang.NullPointerException at spn.setAdapter(arrayAdapter_Contacts);

我在真实设备 ss gt-5570 上进行测试。对不起,我用英语不好:(

4

1 回答 1

1

在您的 sendSMS() 方法中,您正在访问 Activity 的布局

final Spinner spn = (Spinner)findViewById(R.id.spn_contatcs);

这必须为 null,因为您的 Activity 布局不包含 Spinner。您必须执行以下操作(请参阅content.findViewById

Dialog dialog = new Dialog(this);
View content = View.inflate(this, R.layout.dialogsms, null);
// your contact stuff
Spinner spn = (Spinner) content.findViewById(R.id.spn_contatcs);
spn.setAdapter(arrayAdapter_Contacts);
dialog.setContentView(content);
于 2012-04-04T16:51:15.427 回答