【问题标题】:Spinners android getting sendUserActionEvent() mView == null in a fragmentSpinners android 在片段中获取 sendUserActionEvent() mView == null
【发布时间】:2014-10-30 06:34:00
【问题描述】:

我是 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>

【问题讨论】:

  • 你可以在这里发布你的fragment_new_customer.xml吗?
  • 我已经在上面发布了 fragment_new_customer.xml ..
  • 您在哪里设置微调器的适配器?
  • 微调器相关代码在 oncreateview 中。请纠正我并告诉我我错过了什么

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


【解决方案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

        }

    });

}

【讨论】:

  • 感谢您的回答。我必须把这段代码放在fragment中,activity和fragment是一样的吗?
  • 如果我想添加更多微调器,我应该声明更多数组并通过是吗? @Gunaseelan
  • 如何从xml更改数组列表项的文字大小和颜色?
【解决方案2】:

尝试创建一个适配器,然后设置微调器以使用该适配器。快速浏览一下您可以使用的开发者指南:

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);

*考虑将微调器 xml 对象和数组对象的值更改为不同的名称以避免混淆。 **创建一个简单的微调器项目,即微调器列表中的一个项目的外观。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多