Hello!

I work with this library . Spinner has a list of countries with phone codes. I need to get the country code in EditText with a phone number when choosing a country.

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone_book); ccp = (CountryCodePicker) findViewById(R.id.country_cod_picker); phoneNumberEditText = (EditText) findViewById(R.id.phone_number_edit_text); ccp.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() { @Override public void onCountrySelected() { countryCode = ccp.getSelectedCountryCode(); phoneNumberEditText.setText(countryCode); } }); } 

Xml code:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".PhoneBookActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_margin="@dimen/activity_vertical_margin"> <TextView android:layout_weight="3" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/country" android:textColor="@color/black" android:textSize="@dimen/phone_book_text_size"/> <com.hbb20.CountryCodePicker app:textSize="@dimen/phone_book_widgets_text_size" app:showFullName="true" app:defaultNameCode="UA" app:contentColor="@color/black" android:layout_weight="7" android:id="@+id/country_cod_picker" android:layout_width="0dp" android:layout_height="36dp" android:theme="@style/EditTextStyle" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin"> <TextView android:layout_weight="3" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/phone_number" android:textColor="@color/black" android:textSize="@dimen/phone_book_text_size"/> <EditText android:layout_weight="7" android:id="@+id/phone_number_edit_text" android:layout_width="0dp" android:layout_height="wrap_content" android:inputType="phone" android:textSize="@dimen/phone_book_text_size" android:theme="@style/EditTextStyle" /> </LinearLayout> </LinearLayout> 

    1 answer 1

    I understand that the problem is that your code is copied from onCreate , and you are trying to get the countryCode during initialization, before it was actually selected. If this is the case, move the call to ccp.getSelectedCountryCode() inside the ccp.getSelectedCountryCode() event handler and everything should work.

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

    UPD: I looked at the source: You use fragments incorrectly, I advise you to read more on this topic, for example, in official documentation .

    The above XML file does not refer to PhoneBookActivity at all, but to AddFragment and must be initialized there. Move everything that has to do with the selection of a phone code from the activation to the fragment. It should turn out like this:

     public class AddFragment extends Fragment{ private CountryCodePicker ccp; private String countryCode; private EditText phoneNumberEditText; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_add, container, false); ccp = (CountryCodePicker) view.findViewById(R.id.country_code_picker); phoneNumberEditText = (EditText) view.findViewById(R.id.phone_number_edit_text); ccp.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() { @Override public void onCountrySelected() { countryCode = ccp.getSelectedCountryCode(); phoneNumberEditText.setText(countryCode); } }); return view; } } 

    Accordingly, in PhoneBookActivity it is necessary to remove everything that relates to ccp , phoneNumberEditText and countryCode . Checked on Android 6.0.1 - works.

    • Already done so - now the application crashes. I have been sitting for a week and can not understand what is the matter NullPointerException gives. Can you tell me something else? - bogach4312
    • @ bogach4312 Any of the variables means you are null . Add your markup file (XML) and full view initialization code (just copy the contents of your onCreate ) - Agrgg
    • Above all edited. Such an option as it is now, look. - bogach4312
    • @ bogach4312 Everything looks good. ccp and phoneNumberEditText not exactly reset anywhere? - Agrgg
    • I only use them here. Hmm .. but I declare them outside of onCreate (). CountryCodePicker ccp; EditText phoneNumberEditText; - like this. I myself have been a week, every evening I sit and peer and do not understand what is wrong. - bogach4312