无法将片段的子类作为片段返回

时间:2015-06-01 03:13:56

标签: android android-activity fragment

我有一个名为SingleFragmentActivity的类,我希望我的类WorkoutActivity能够扩展。当我用该方法中的代码覆盖抽象方法createFragment时,我得到一个错误,当我运行代码时,我得到---

  

错误:(12,8)错误:WorkoutActivity不是抽象的,也不是   在SingleFragmentActivity中覆盖抽象方法createFragment()   错误:(16,24)错误:WorkoutActivity中的createFragment()不能   在SingleFragmentActivity返回类型中覆盖createFragment()   android.app.Fragment与。不兼容   android.support.v4.app.Fragment错误:(15,5)错误:方法没有   从超类型错误:(20,43)错误覆盖或实现方法:   不兼容的类型:WorkoutFragment无法转换为Fragment

我相信我的代码是正确的,因为我扩展了这个类并在我的应用程序的其他几个地方以相同的方式覆盖了这个方法。这是我参与功能的代码。

public class WorkoutFragment extends Fragment
{
    public static final String EXTRA_ALREADYCREATED_ID = "alreadycreated";

    private ExAdapter adapter;
    private ListView listView;
    private ArrayList<Set> sets;
    private Workout w;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UUID workoutId = (UUID) getArguments().getSerializable(EXTRA_ALREADYCREATED_ID);
        w = WorkoutMASTER.get(getActivity()).getWorkout(workoutId);
    }


    public View onCreateView(LayoutInflater inflater, ViewGroup parent,Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.fragment_workout, parent, false);
        return v;

    }
    public static WorkoutFragment newInstance(UUID workoutId)
    {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_ALREADYCREATED_ID, workoutId);
        WorkoutFragment fragment = new WorkoutFragment();
        fragment.setArguments(args);

        return fragment;
    }
}

public class WorkoutActivity extends SingleFragmentActivity {

    @Override
    protected Fragment createFragment()
    {
        UUID createdWorkoutId = (UUID) getIntent().getSerializableExtra(WorkoutFragment.EXTRA_ALREADYCREATED_ID);

        return WorkoutFragment.newInstance(createdWorkoutId);
    }
}


public abstract class SingleFragmentActivity extends FragmentActivity
{
    protected abstract Fragment createFragment();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        FragmentManager fm = getSupportFragmentManager(); //calling support part to make it compatible with honeycomb devices...
        //otherwise, just call getFragmentManager
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

        if(fragment == null)
        {
            fragment =  createFragment();
            fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
        }
    }
}

我排除了我的导入,但它们存在于我的项目中。 在运行项目之前,错误发生在WorkoutActivity中createFragment()的return语句中。我非常感谢任何帮助。

0 个答案:

没有答案