spinner
中使用fragment
时,我收到了这些错误## //my fragments code
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import static android.R.layout.simple_spinner_dropdown_item;
public class lang_spinner extends Fragment{
Spinner source_lang;
Spinner target_lang;
String array_sourcelang[];
String array_targetlang[];
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.lang_spinner, container, false);
source_lang = (Spinner) view.findViewById(R.id.sourcelang);
array_sourcelang = new String[]{"English", "German"};
ArrayAdapter<String> arrayAdapter1;
arrayAdapter1 = new ArrayAdapter<String>(getActivity(),
simple_spinner_dropdown_item, array_sourcelang);
source_lang.setAdapter(arrayAdapter1);
target_lang = (Spinner) view.findViewById(R.id.targetlang);
array_targetlang = new String[]{"English", "German"};
ArrayAdapter<String> arrayAdapter2 = new ArrayAdapter<String>(getActivity(),
simple_spinner_dropdown_item, array_targetlang);
target_lang.setAdapter(arrayAdapter2);
return view;
}
//Errors
Error:(20, 50) error: incompatible types: View cannot be converted to Spinner
Error:(26, 50) error: incompatible types: View cannot be converted to Spinner
Error:(30, 20) error: cannot find symbol method setAdapter(ArrayAdapter<String>)
Error:(25, 20) error: cannot find symbol method setAdapter(ArrayAdapter<String>)
//my xaml file lang_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_spinner"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.sarimahmed.fyp.Spinner">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sourcelang"
android:layout_weight="5"
android:popupBackground="@color/spinnerbackground" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/langswapbtn"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/btn_swap1"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:popupBackground="@color/spinnerbackground"
android:id="@+id/targetlang"
android:layout_weight="5"
android:textColor="@color/mytextcolor"
/>
</LinearLayout>
答案 0 :(得分:2)
不兼容的类型:Spinner无法转换为String
因为source_lang
和target_lang
声明为String
。
在声明Spinner
和String
变量时使用source_lang
代替target_lang
:
Spinner source_lang;
Spinner target_lang;
答案 1 :(得分:0)
你需要改变String source_lang;到Spinner source_lang;和String target_lang;到Spinner target_lang;
答案 2 :(得分:0)
删除此行。
0deg
试试这个,
import static android.R.layout.simple_spinner_dropdown_item;