简体   繁体   中英

How to initiate TextView in a fragment?

I am new to fragments and have the following code:

public class TripleTab1 extends Fragment {

private TextView nameAndNumberText;
private Map<String, String> nameToNumberMapping;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState){

    nameAndNumberText = (TextView) getActivity().findViewById(R.id.numberToNameView);
    nameToNumberMapping = ContactNameAndNumberUtility
            .getNameToPhoneNumberMapping(getActivity()
            .getApplicationContext());

    for (Map.Entry<String, String> item : nameToNumberMapping.entrySet()) {
        String key = item.getKey();
        String value = item.getValue();

        nameAndNumberText.append(key);
        nameAndNumberText.append(value);
    }
    return inflater.inflate(R.layout.assign_pattern_tab_1, container, false);

}

}

Unfortunately, this gives me a NullPointerException. It seems like the TextView object is not initiated, even though I believe it should be.

"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference"

Does anyone know what could be wrong?

Replace your code with this

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState){
    View view =inflater.inflate(R.layout.assign_pattern_tab_1, container, false);
    nameAndNumberText = (TextView) view.findViewById(R.id.numberToNameView);
    nameToNumberMapping = ContactNameAndNumberUtility
            .getNameToPhoneNumberMapping(getActivity()
            .getApplicationContext());

    for (Map.Entry<String, String> item : nameToNumberMapping.entrySet()) {
        String key = item.getKey();
        String value = item.getValue();

        nameAndNumberText.append(key);
        nameAndNumberText.append(value);
    }
    return view;

you should do like this:

Need to pass view to your widgets instead of activity and you are inflating your view at the end take it to the top.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState){
  View v = inflater.inflate(R.layout.assign_pattern_tab_1, container, false);
 nameAndNumberText = (TextView) v.findViewById(R.id.numberToNameView);
nameToNumberMapping = ContactNameAndNumberUtility
        .getNameToPhoneNumberMapping(getActivity()
        .getApplicationContext());

for (Map.Entry<String, String> item : nameToNumberMapping.entrySet()) {
    String key = item.getKey();
    String value = item.getValue();

    nameAndNumberText.append(key);
    nameAndNumberText.append(value);
}
return v;

You need to create a View first

View view = inflater.inflate(R.layout.assign_pattern_tab_1, container,false); 
nameAndNumberText = (TextView) view.findViewById(R.id.numberToNameView);
        nameToNumberMapping = ContactNameAndNumberUtility
                .getNameToPhoneNumberMapping(getActivity()
                .getApplicationContext());

        for (Map.Entry<String, String> item : nameToNumberMapping.entrySet()) {
            String key = item.getKey();
            String value = item.getValue();

            nameAndNumberText.append(key);
            nameAndNumberText.append(value);
        }
        return inflater.inflate(R.layout.assign_pattern_tab_1, container, false);

You should do like this:-

public class TripleTab1 extends Fragment {

private TextView nameAndNumberText;
private Map<String, String> nameToNumberMapping;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState){
View v = inflater.inflate(R.layout.assign_pattern_tab_1, container, false);
    nameAndNumberText = (TextView) v.findViewById(R.id.numberToNameView);
    nameToNumberMapping = ContactNameAndNumberUtility
            .getNameToPhoneNumberMapping(getActivity()
            .getApplicationContext());

    for (Map.Entry<String, String> item : nameToNumberMapping.entrySet()) {
        String key = item.getKey();
        String value = item.getValue();

        nameAndNumberText.append(key);
        nameAndNumberText.append(value);
    }
    return v;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM