覆盖instantiateItem - 不兼容的返回类型

时间:2016-03-21 17:10:59

标签: android wear-os android-pageradapter

我正在尝试覆盖扩展instantiateItem的类中的方法FragmentGridPgerAdapter

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.wearable.view.CardFragment;

import android.support.wearable.view.FragmentGridPagerAdapter;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;

public class PickerAdapter extends FragmentGridPagerAdapter
{
    ...
    @Override
    protected Object instantiateItem(ViewGroup viewGroup, int row, int col)
    {
        TextView textView = new TextView(mContext);
        textView.setText("Ahoj");

        viewGroup.addView(textView);
        return textView;
    }
    ...
  } 

但是Android Studio在单词Object上给出了错误:

'instantiateItem(ViewGroup, int, int)' in 'com.example.sk1x1.myapplication.PickerAdapter' clashes with 'instantiateItem(ViewGroup, int, int)' in 'android.support.wearable.view.FragmentGridPagerAdapter'; attempting to use incompatible return type

如果我运行程序,在构建期间我得到:

Error:(78, 22) error: instantiateItem(ViewGroup,int,int) in PickerAdapter cannot override instantiateItem(ViewGroup,int,int) in GridPagerAdapter
attempting to assign weaker access privileges; was public

我做错了什么?

2 个答案:

答案 0 :(得分:0)

Error:(78, 22) error: instantiateItem(ViewGroup,int,int) in PickerAdapter cannot override instantiateItem(ViewGroup,int,int) in GridPagerAdapter
attempting to assign weaker access privileges; was public

更改

@Override
protected Object instantiateItem(ViewGroup viewGroup, int row, int col)

@Override
public Object instantiateItem(ViewGroup viewGroup, int row, int col)

答案 1 :(得分:0)

像@Blackbelt所说,你应该有下面的函数定义,你应该返回你想要返回的片段:一个自定义的片段或一个卡片段(如果你想要的话)(在CardFragment的一个例子下面)。

@Override
public Fragment instantiateItem(ViewGroup container, int row, int column) {
    return CardFragment.create("title", "text", R.drawable.ic_your_icon);
}

new Fragment()将返回一个空片段。

在Android Studio上,使用Ctrl + o覆盖某个功能,您将拥有正确的定义。