gpt4 book ai didi

java - 方法需要对象并从扩展类中查找对象

转载 作者:行者123 更新时间:2023-11-29 20:38:34 25 4
gpt4 key购买 nike

我想在我的 Android 应用程序 Activity 中创建可以滑动浏览的 fragment 。更具体地说,这些 fragment 包含图像,我想浏览这些图像。

现在,为了实现这一点,我使用了 FragmentPagerAdapter。我现在遇到的问题是,我在扩展 FragmentPagerAdaptor 的类中的一个方法需要返回一个 Fragment 对象,但根据编译器,它找到了一个不同的对象。这很奇怪,因为我返回的对象来自扩展 Fragment 的类,因此在我看来它应该可以工作。

有问题的方法是 SlidesPageAdaptor 类中的 getItem(int position) 错误是

Incompatible types
Required: android.support.v4.app.Fragment
Found: ourpackagename.fragment_slides

SlidesPageAdaptor.java

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class SlidesPageAdaptor extends FragmentPagerAdapter {

String[] devices;
String[] deviceDescription;

public SlidesPageAdaptor(FragmentManager fm, Context context) {
super(fm);

Resources resources = context.getResources();

devices = resources.getStringArray(R.array.devices);
deviceDescription = resources.getStringArray(R.array.device_description);
}

@Override
public Fragment getItem(int position) {

Bundle bundle = new Bundle();
bundle.putString(fragment_slides.DescriptionKey, deviceDescription[position]);
bundle.putInt(fragment_slides.ImageIDKey, getImageID(position));

fragment_slides fragmentSlide = new fragment_slides();
fragmentSlide.setArguments(bundle);

return fragmentSlide;
}

private int getImageID(int position) {

int id = 0;
switch (position)
{
case 0:
id = R.drawable.abc_btn_check_material;
break;
case 1:
id = R.drawable.abc_ab_share_pack_mtrl_alpha;
break;
case 2:
id = R.drawable.abc_btn_radio_material;
break;
}

return id;
}

@Override
public CharSequence getPageTitle(int position) {
return devices[position];
}

@Override
public int getCount() {
return devices.length;
}

还有 fragment_slides.class(我知道这个名字不好,但现在不重要):

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class fragment_slides extends Fragment {

public static final String ImageIDKey = "imagekey";
public static final String DescriptionKey = "descriptionkey";

public fragment_slides() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Inflate the layout for this fragment

View view = inflater.inflate(R.layout.fragment_slides_images, container, false);

Bundle bundle = getArguments();

if(bundle != null)
{
int imageID = bundle.getInt(ImageIDKey);
String description = bundle.getString(DescriptionKey);

setValues(view, imageID, description);
}

return view;

//return inflater.inflate(R.layout.fragment_slides_images, container, false);
}

private void setValues(View view, int imageID, String description) {
ImageView imageview = (ImageView) view.findViewById(R.id.imageViewSlide);
imageview.setImageResource(imageID);

TextView textView = (TextView) view.findViewById(R.id.tvSlideTest);
textView.setText(description);
}

我认为不需要其他 Activity/类(class),但如果需要我可以提供。

最佳答案

fragment_slides.class 中,您应该导入 android.support.v4.app.Fragment,而不是 android.app.Fragment

关于java - 方法需要对象并从扩展类中查找对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com