Spinners android在片段中获取sendUserActionEvent()mView == null

时间:2014-10-30 06:34:00

标签: android android-fragments android-studio android-spinner

我是Android新手,虽然我用谷歌搜索我无法解决以下问题.. 所以请帮忙!

当我点击我的微调器时,我收到sendUserActionEvent() mView == null错误。

下面的

是我的new_customer.xml微调器及其在Fragment中的适配器。

   <ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:columnCount="1"
    android:rowCount="9"
    tools:context="com.sensei.mencore.NewCustomerFragment">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/customer_id"
        android:hint="Customer ID"
        style="@style/style_edit_text_default"

        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/first_name"
        android:hint="First Name"
        style="@style/style_edit_text_default"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/middle_name"
        android:hint="Middle Name"
        style="@style/style_edit_text_default"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/second_name"
        android:hint="Last Name"
        style="@style/style_edit_text_default"
        />


     <RelativeLayout

    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Date of Birth"
            style="@style/style_edit_text_default"
            android:inputType="date"
            android:id="@+id/customer_date_picker"/>

        <ImageView
            android:id="@+id/CalenderView"
            android:padding="5dp"
            android:layout_width="35dp"
            android:layout_height="dp"
            android:layout_alignTop="@+id/customer_date_picker"
            android:layout_alignBottom="@+id/customer_date_picker"
            android:layout_alignRight="@+id/customer_date_picker"
            android:onClick="ShowDateDlg"
            android:src="@drawable/calendar"
            />
    </RelativeLayout>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:maxLines="10"
        style="@style/style_edit_text_default"
        android:hint="Addressline 1"
        android:scrollbars="vertical"  />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:maxLines="10"
        style="@style/style_edit_text_default"
        android:hint="Addressline 2"
        android:scrollbars="vertical"  />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:maxLines="10"
        style="@style/style_edit_text_default"
        android:hint="Addressline 3"
        android:scrollbars="vertical"  />


    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          android:id="@+id/country_code"
        android:hint="Country code"
        style="@style/style_edit_text_default"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/state_code"
        android:hint="State code"
        style="@style/style_edit_text_default"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/city_code"
        android:hint="City code"
        style="@style/style_edit_text_default"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/district_code"
        android:hint="District code"
        style="@style/style_edit_text_default"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/branch_code"
        android:hint="Branch code"
        style="@style/style_edit_text_default"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pin_code"
        android:hint="Pin code"
        android:inputType="number"
        style="@style/style_edit_text_default"
        />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/style_btn_default"
        android:text="Create"
        android:id="@+id/create_button"
        />



</GridLayout>

Fragment和strings.xml在这里---

 public class NewCustomerFragment extends Fragment {
    EditText customerDobEditText;
    SimpleDateFormat sdf;
    Spinner sp;
    Calendar calendar = Calendar.getInstance();
   DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
    @Override

    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

        calendar.set(Calendar.YEAR,year);

        calendar.set(Calendar.MONTH,monthOfYear);

        calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);

        updateDate();
    }
};
public void updateDate(){
    String myFormat="yyyy/MM/dd";
    sdf = new SimpleDateFormat(myFormat, Locale.US);
    customerDobEditText.setText(sdf.format(calendar.getTime()));

public static NewCustomerFragment newInstance() {
    NewCustomerFragment fragment = new NewCustomerFragment();
    return fragment;
}

public NewCustomerFragment() {  }

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_new_customer, container, false);

    sp = (Spinner) v.findViewById(R.id.country_code);
    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {

            Toast.makeText(getActivity(), sp.getSelectedItem().toString(),
                    Toast.LENGTH_SHORT).show();
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

    });
    return v;
}





 <?xml version="1.0" encoding="utf-8"?>
<resources>
 <string-array name="country_code">
    <item>Item 1</item>
    <item>Item 2</item>
    <item>Item 3</item>
    <item>Item 4</item>
    <item>Item 5</item>
</string-array>

2 个答案:

答案 0 :(得分:1)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_new_customer, container, false);
    return v;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    sp = (Spinner) getView().findViewById(R.id.country_code);
    String values[] = getResources().getStringArray(R.array.country_code);
    ArrayAdapter<String> adapter= new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    sp.setAdapter(adapter);
    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {

            Toast.makeText(getActivity(), sp.getSelectedItem().toString(),
                    Toast.LENGTH_SHORT).show();
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

    });

}

试试这种方式,我希望这能解决你的问题

<强>更新

您也可以从xml中设置数据..

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

从代码中移除适配器......

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    sp = (Spinner) getView().findViewById(R.id.country_code);

    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {

            Toast.makeText(getActivity(), sp.getSelectedItem().toString(),
                    Toast.LENGTH_SHORT).show();
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

    });

}

答案 1 :(得分:1)

尝试创建适配器,然后设置微调器以使用该适配器。只需快速查看开发人员指南,您就可以使用:

Spinner spinner = (Spinner)v.findItemById(R.id.country_code); // *
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
    R.array.country_code, android.R.layout.simple_spinner_item); // **
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
spinner.setAdapter(adapter);

*考虑将spinner xml对象和数组对象的vaules更改为不同的名称,以避免混淆。 **创建一个简单的微调器项,即微调器列表中的一个项看起来像。

希望这有帮助。