2017-02-20 7 views
0

私はこれをlibraryと一緒に使っています。私はSpinnerの国の電話番号で国リストを持っています。国を選択するときには、国コードをEditTextフィールドに表示する必要があります。これは私のコードです:EditTextを使ったぼかしスピナー

CountryCodePicker ccp; 
String countryCode; 
EditText phoneNumberEditText; 

ccp = (CountryCodePicker) findViewById(R.id.country_cod_picker); 
final CountryCodePicker ccp = new CountryCodePicker(this); 
phoneNumberEditText = (EditText) findViewById(R.id.phone_number_edit_text); 
countryCode = ccp.getSelectedCountryCode(); 
ccp.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() { 
    @Override 
    public void onCountrySelected() { 
     phoneNumberEditText.setText(countryCode); 
    } 
}); 

私のアプリはクラッシュしませんが、コントリーコードは表示されません。お願い助けて!ありがとうございました。

答えて

0

1. そうでない場合は、あなたのcountryCode変数はまだ古いを持って、あなたはコールバックでccp.getSelectedCountryCode()を呼び出す必要が

ccp.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() { 
    @Override 
    public void onCountrySelected() { 
     countryCode = ccp.getSelectedCountryCode(); 
     phoneNumberEditText.setText(countryCode); 
} 
}); 

を次のようにコードを修正し

final CountryCodePicker ccp = new CountryCodePicker(this); 

2. この行を削除します割り当てられた値。

+0

私はそれをやった - 今私のアプリケーションがクラッシュします。私はjava.lang.NullPointerExceptionとjava.lang.RuntimeExceptionを持っています – bogach4312

0

問題点:「最終的なCountryCodePicker ccp = new CountryCodePicker(this);」という問題があります。前の行で設定したccp変数を非表示にします。私は、新しいCountryCodePickerを作成する必要性を理解していません。 findViewByIdは有効なCountryCodePickerを返す必要があります(CountryCodePickerをレイアウトに追加したと仮定します)。私は、 "最終的なCountryCodePicker ccp = new CountryCodePicker(this);"という行を削除します。

findViewById()がnullを返すと、レイアウトに「country_cod_picker」が存在しないことが示されます。私はcountry_cod_pickerが誤っていると推測しています。代わりに、行に "R.id.country_code_picker"を読み込まなければなりません(文字eがありません)。

関連する問題