【发布时间】: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