如何在导航抽屉活动中使用阵列适配器将项目添加到微调器



我在简单活动中使用它时,我为我的项目使用字符串.xml它可以正常工作,但是当我想在导航抽屉活动中使用它时,它确实有效。 错误是

错误:(98, 51( 错误: 不兼容的类型: 联系我们片段不能 转换为上下文 错误:任务":app:compileDebugJavaWithJavac"的执行失败。 编译失败;有关详细信息,请参阅编译器错误输出。

这是我的字符串.xml

<resources>
<string-array name="country_names">
<item> select </item>
<item>germany</item>
<item>italy</item>
<item>metro</item>
</string-array>
</resources>

我在这一行有问题:

adapter = ArrayAdapter.createFromResource(this, R.array.country_names, 人造人。R.layout.simple_spinner_item(;

public class ContactUsFragment extends Fragment implements OnMapReadyCallback  {
GoogleMap map;
public ContactUsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate( fragment_contact_us, container, false);
return v;
} //end of onCreateView
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated( view, savedInstanceState );

spinner = (Spinner) getView().findViewById( R.id.spinner );
adapter = ArrayAdapter.createFromResource(this, R.array.country_names, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
SupportMapFragment mapFragment = (SupportMapFragment)
getChildFragmentManager().findFragmentById( R.id.map1 );
mapFragment.getMapAsync( this );
} // end of onViewCreated
@Override
public  void    onMapReady(GoogleMap googleMap){
map=googleMap;
LatLng pp=new LatLng(12,25);
MarkerOptions option=new MarkerOptions();
option.position(pp).title("hi ");
map.addMarker(option);
map.moveCamera(CameraUpdateFactory.newLatLng(pp));
}
}//end of class

如果需要context,则必须通过activityapplication。在这种情况下,我认为activity就足够

了更改此行:

adapter = ArrayAdapter.createFromResource(this, R.array.country_names, android.R.layout.simple_spinner_item);

为此

adapter = ArrayAdapter.createFromResource(getActivity(), R.array.country_names, android.R.layout.simple_spinner_item);

注意getActivity()调用

最新更新