2015-07-04 65 views
1

我想在我的Android应用程序活动中创建可以滑过的片段。更具体地说,这些片段包含图像,我想滑过这些。方法需要对象并从扩展类中查找对象

现在要完成此操作,我使用FragmentPagerAdapter。我现在遇到的问题是,我在扩展FragmentPagerAdaptor的类中的一个方法需要返回一个Fragment对象,但根据编译器它会找到一个不同的对象。 这很奇怪,因为我返回的对象是来自扩展片段的类,因此它应该在我看来有效。

该问题的方法是在SlidesPageAdaptorgetItem(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); 
} 

}

我不认为其他活动/类是必需的,但我可以在需要时提供它们。

回答

2

fragment_slides.class你应该导入android.support.v4.app.Fragment,不android.app.Fragment

+1

这是导致问题的唯一问题。你为什么包括_also _? – Codebender

+0

我的不好,固定... –

+0

谢谢你!这样的错误,应该看到.. – Someguy

0

的问题是在这里:

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

既然你声明fragment_slides对象,而不是一个片段对象,编译器认为它不是一个片段。试试这个:

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