Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

java - Incompatible types error with Fragments

I'm trying to add a fragment to my main activity. When I type fragment = new CrimeFragment(); I get an Incompatible types error.

I tried casting new CrimeFragment(); to a Fragment, but that didn't work.

package com.adamanteusstudios.officesmostwanted;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;


public class MostWantedActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_most_wanted);

        //getting fragmentManager
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

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



}

The error says:

incompatible types
required: android.v4.support.app.Fragment
found: com.adamanteusstudios.officesmostwanted.OffenseFragment

How do I fix this error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In class OffenseFragment.java replace

import android.app.Fragment;

with

import android.support.v4.app.Fragment;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...