以下是Java代码
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_create_report, container, false);
activity = getActivity();
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinnerComplaintType);
ArrayAdapter<String> adapterComplaint = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, R.array.complaintType);
adapterComplaint.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapterComplaint);
spinnerComplaintType.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
complaintType = parent.getItemAtPosition(position).toString();
// Toast.makeText(getApplicationContext(), complaintType, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return rootView;
}
这是XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="833dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="24dp"
android:layout_marginTop="54dp"
android:text="Complaint Type: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/spinnerComplaintType"
android:layout_marginTop="15dp"
android:text="Vehicle Type"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinnerVehicleType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView3"
android:layout_toRightOf="@+id/textView2"
android:textColor="#000000" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/spinnerVehicleType"
android:layout_marginTop="21dp"
android:text="Body No:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editTextBodyNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView4"
android:layout_centerHorizontal="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editTextPlate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextBodyNo"
android:layout_alignTop="@+id/textView5"
android:ems="10" />
<EditText
android:id="@+id/editTextDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextLocation"
android:layout_alignTop="@+id/textView7"
android:ems="10" />
<EditText
android:id="@+id/editTextComplaintDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextDate"
android:layout_alignTop="@+id/textView8"
android:ems="10"
android:inputType="textMultiLine" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView5"
android:layout_below="@+id/textView5"
android:layout_marginTop="58dp"
android:text="Location"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="55dp"
android:text="Plate No:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_below="@+id/editTextLocation"
android:layout_marginTop="30dp"
android:text="Date of Incident"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView7"
android:layout_below="@+id/editTextDate"
android:layout_marginTop="28dp"
android:text="Complaint Details"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editTextLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextPlate"
android:layout_alignTop="@+id/textView6"
android:ems="10" />
<ImageView
android:id="@+id/imageViewPhotoReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonCamera"
android:layout_below="@+id/buttonCamera"
android:src="@drawable/about"
android:visibility="invisible"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView6"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:text="File Complaint"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/buttonCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonSend"
android:layout_toLeftOf="@+id/spinnerVehicleType"
android:text="Take Photo" />
<Button
android:id="@+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextComplaintDetails"
android:layout_marginTop="48dp"
android:layout_toRightOf="@+id/textView1"
android:text="SEND" />
<Button
android:id="@+id/buttonDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextComplaintDetails"
android:layout_below="@+id/editTextComplaintDetails"
android:layout_marginTop="20dp"
android:text="pick date" />
<Spinner
android:id="@+id/spinnerComplaintType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_toRightOf="@+id/textView3"
android:textColor="#000000" />
</RelativeLayout>
</ScrollView>
这是错误 记录跟踪
01-04 21:18:36.513: E/AndroidRuntime(3772): FATAL EXCEPTION: main
01-04 21:18:36.513: E/AndroidRuntime(3772): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Spinner
01-04 21:18:36.513: E/AndroidRuntime(3772): at info.androidhive.slidingmenu.CreateReportFragment.onCreateView(CreateReportFragment.java:31)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:830)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1034)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.BackStackRecord.run(BackStackRecord.java:622)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1384)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:428)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.os.Handler.handleCallback(Handler.java:605)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.os.Handler.dispatchMessage(Handler.java:92)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.os.Looper.loop(Looper.java:154)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.ActivityThread.main(ActivityThread.java:4945)
01-04 21:18:36.513: E/AndroidRuntime(3772): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 21:18:36.513: E/AndroidRuntime(3772): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 21:18:36.513: E/AndroidRuntime(3772): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-04 21:18:36.513: E/AndroidRuntime(3772): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-04 21:18:36.513: E/AndroidRuntime(3772): at dalvik.system.NativeStart.main(Native Method)
所以我能够在一个活动中使 Spinner 工作,但是当我将代码转移到片段时,它显然没有& #39; t work。
我认为错误来自Spinner的Instatiation。
答案 0 :(得分:1)
将此选项用于 TextView 和 Spinner 。
<Spinner
android:id="@+id/spinnerComplaintType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="54dp"
android:layout_toRightOf="@+id/buttonSend"
android:textColor="#000000" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_alignTop="@+id/spinnerComplaintType"
android:layout_marginTop="17dp"
android:text="File Complaint"
android:textAppearance="?android:attr/textAppearanceLarge" />
同时更改
android.R.layout.simple_spinner_item
而不是
android.R.layout.simple_list_item_1
数组适配器中的。
更新:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.complaintType, android.R.layout.simple_spinner_item);