setText TextView里面的片段

时间:2013-12-09 19:51:51

标签: android variables android-fragments android-spinner

我有一个带有微调器,按钮和textview的片段。

我的旋转器有一串Mansanas和Pakwan。

我无法解释清楚。我的代码只是解释我想做什么。大声笑。这是我的代码:

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)

2 个答案:

答案 0 :(得分:3)

你不能使用这一行。在这种情况下,v是您点击的按钮。

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

将其替换为

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

获取片段的视图。

答案 1 :(得分:1)

你有

public class FragmentA extends Fragment {
Spinner spinner1; 

更改此

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

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