试图从微调器获取数据,但它的NULL

时间:2014-02-15 01:27:44

标签: android android-fragments android-spinner android-xml

我有一个片段,上面有几个微调器,用户可以从中选择一些选项,底部有一个按钮。我有按钮设置的onClick来从微调器获取数据,然后将其发送到带有asyncTask的数据库。

片段xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tastePickTitle"
        android:text="Select a Taste: "
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="20sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/taste_array"

        />

    <View
        android:layout_width="fill_parent"
        android:layout_height="30dp">
    </View>

    <TextView
        android:id="@+id/ammountPickTitle"
        android:text="How much taste: "
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="20sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/ammount_array"

        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >



        <Button
            android:id="@+id/addTasteButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Taste"
            android:layout_weight="1"
            />




    </LinearLayout>




</LinearLayout>

片段代码是:

public class AddTastePage extends Fragment {

    String beerId = "";
    String url = "";

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

        //set layout here
        View v = inflater.inflate(R.layout.add_taste_layout, container, false);
        setHasOptionsMenu(true);
        getActivity().setTitle("Style");

        Bundle args = getArguments();
        beerId = args.getString("beerIDSent");

        //get user information
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        final String userID = prefs.getString("userID", null);


        //set button on click

        //add button onclick fo top beers
        Button bt = (Button)v.findViewById(R.id.addTasteButton);
        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //todo: send taste tag to server
                //get spinner text
                Spinner tasteSpinner = (Spinner)v.findViewById(R.id.spinner1);
                String tasteText = tasteSpinner.getSelectedItem().toString();

                Spinner ammountSpinner = (Spinner) v.findViewById(R.id.spinner2);
                String ammountText = ammountSpinner.getSelectedItem().toString();


                int ammount = 0;
                if(ammountText.equals("A Hint")){
                    ammount = 1;
                }
                if(ammountText.equals("Medium")){
                    ammount = 2;
                }
                if(ammountText.equals("Strong")){
                    ammount = 3;
                }

                //construct url
                String url = "myURL";



                new UpdateTaste(getActivity()).execute(url);

                //todo: send back to taste list fragment, possible launch a save confirmation
                Fragment Fragment_three;
                FragmentManager man= getFragmentManager();
                FragmentTransaction tran = man.beginTransaction();
                Fragment_three = new TasteTags2();
                final Bundle bundle = new Bundle();
                bundle.putString("beerIDSent", beerId);
                Fragment_three.setArguments(bundle);
                tran.replace(R.id.main, Fragment_three);//tran.
                tran.addToBackStack(null);
                tran.commit();

            }
        });





        // Inflate the layout for this fragment
        return v;

    }


}

FC错误:

02-14 20:29:59.102  19523-19523/com.beerportfolio.beerportfoliopro E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
02-14 20:30:21.266  19523-19523/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.beerportfolio.beerportfoliopro.AddTastePage$1.onClick(AddTastePage.java:55)
            at android.view.View.performClick(View.java:4280)
            at android.view.View$PerformClick.run(View.java:17984)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)

AddTastePage中的第55行是:

String ammountText = ammountSpinner.getSelectedItem().toString();

2 个答案:

答案 0 :(得分:1)

你有v的2个声明,他们可能会混淆起来。你有这个v

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

然后你有一个v就是你点击的按钮

@Override
public void onClick(View v)

这样v就不会有你的微调器导致你的微调器变为null,因为它找不到它

将onClick一个重命名为其他内容,您可能还必须制作第一个v,这是您的布局最终版,因为您在onClick

中访问它

答案 1 :(得分:0)

您只需要声明Spinner以上setOnClickListener 试试这段代码:

        //add button onclick fo top beers
    Button bt = (Button)v.findViewById(R.id.addTasteButton);
    final Spinner tasteSpinner = (Spinner)v.findViewById(R.id.spinner1);
    final Spinner ammountSpinner = (Spinner) v.findViewById(R.id.spinner2);
    bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //todo: send taste tag to server
            //get spinner text

            String tasteText = tasteSpinner.getSelectedItem().toString();


            String ammountText = ammountSpinner.getSelectedItem().toString();