Reputation: 55
I am new to android,though i googled i couldn't resolve this below problem.. so please help out!
I am getting sendUserActionEvent() mView == null
error when i click on my spinner.
below is my new_customer.xml spinner and its adapter in 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 and strings.xml are here ---
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>
Upvotes: 1
Views: 7524
Reputation: 15535
@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
}
});
}
Try this way, I hope this will solve your problem
Update
You can set data from your xml also..
<Spinner
android:id="@+id/country_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/country_code" />
And remove adapter from your 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
}
});
}
Upvotes: 1
Reputation: 1463
Try creating an adapter and then setting the spinner to use that adapter. Just quickly looking through the developer guide you could use:
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);
*Consider changing the vaules of both the spinner xml object and the array object to be different names to avoid confusion. **create a simple spinner item i.e. what one item on the spinner list would look like.
Hope this helps.
Upvotes: 1