setText TextView inside fragment

2019-09-02 02:29发布

问题:

I have a fragment with a spinner, button and a textview.

My spinner has a string of Mansanas and Pakwan.

I can't explain it clearly. My code will just explain what i want to do. lol. Here's my code:

FragmentA:

public class FragmentA extends Fragment {

    Spinner spinner1;

    Button button2;
    TextView textView6;

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

        View v = inflater.inflate(R.layout.fragmenta, container, false);

        textView6 = (TextView)v.findViewById(R.id.text6);
        button2 = (Button)v.findViewById(R.id.button2);
        button2.setOnClickListener(Click);

        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        spinner1 = (Spinner) getActivity().findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence>  adapter_a = ArrayAdapter.createFromResource(getActivity(), R.array.spinner1,android.R.layout.simple_spinner_item );
        spinner1.setAdapter(adapter_a);
    }

    OnClickListener Click = new OnClickListener(){ 

        @Override
        public void onClick(View v) {

            int a;

            Spinner spinner1 = (Spinner) v.findViewById(R.id.spinner1);
            String s1 = spinner1.getSelectedItem().toString();

            if(s1.equals("Mansanas")) {
                a = 5; 
                textView6.setText(Integer.toString(a)); 
            }
            else if(s1.equals("Pakwan")) {
                a = 10; 
                textView6.setText(Integer.toString(a)); 
            }   
        }   
    };

}

LogCat:

12-10 03:50:34.601: E/AndroidRuntime(31368): FATAL EXCEPTION: main
12-10 03:50:34.601: E/AndroidRuntime(31368): java.lang.NullPointerException
12-10 03:50:34.601: E/AndroidRuntime(31368):    at ljc.test.FragmentA$1.onClick(FragmentA.java:51)
12-10 03:50:34.601: E/AndroidRuntime(31368):    at android.view.View.performClick(View.java:4212)
12-10 03:50:34.601: E/AndroidRuntime(31368):    at android.view.View$PerformClick.run(View.java:17476)
12-10 03:50:34.601: E/AndroidRuntime(31368):    at android.os.Handler.handleCallback(Handler.java:800)
12-10 03:50:34.601: E/AndroidRuntime(31368):    at android.os.Handler.dispatchMessage(Handler.java:100)
12-10 03:50:34.601: E/AndroidRuntime(31368):    at android.os.Looper.loop(Looper.java:194)
12-10 03:50:34.601: E/AndroidRuntime(31368):    at android.app.ActivityThread.main(ActivityThread.java:5371)
12-10 03:50:34.601: E/AndroidRuntime(31368):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 03:50:34.601: E/AndroidRuntime(31368):    at java.lang.reflect.Method.invoke(Method.java:525)
12-10 03:50:34.601: E/AndroidRuntime(31368):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-10 03:50:34.601: E/AndroidRuntime(31368):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-10 03:50:34.601: E/AndroidRuntime(31368):    at dalvik.system.NativeStart.main(Native Method)

回答1:

You cannot use this line. v in this case is the button you clicked.

Spinner spinner1 = (Spinner) v.findViewById(R.id.spinner1);

Replace that with

Spinner spinner1 = (Spinner) getView().findViewById(R.id.spinner1);

to get the fragment's view.



回答2:

You have

public class FragmentA extends Fragment {
Spinner spinner1; 

Change this

Spinner spinner1 = (Spinner) v.findViewById(R.id.spinner1);

to

spinner1 = (Spinner) getView().findViewById(R.id.spinner1);