" FragmentLandscape"无法转换为Fragment

时间:2015-07-11 04:11:09

标签: java android android-fragments

我遵循了关于多个布局片段的教程,但是我收到了一个错误,我不知道它是什么。我已经搜索了v4的内容,但我无法理解它并且我不知道如何将其应用到我的代码中

package com.example.renboy94.fragmentsresponsive;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;


public class MainActivity extends Activity{

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

        FragmentManager fragmentManager = getFragmentManager();

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        Configuration configInfo = getResources().getConfiguration();

        if(configInfo.orientation == Configuration.ORIENTATION_LANDSCAPE){
            FragmentLandscape fragmentLandscape = new FragmentLandscape();

            fragmentTransaction.replace(R.id.landscape_fragment, fragmentLandscape);
        }else{
            FragmentPortrait fragmentPortrait = new FragmentPortrait();

            fragmentTransaction.replace(R.id.portrait_fragment, fragmentPortrait);
        }

        fragmentTransaction.commit();

    }

}

FragmentLandscape类

package com.example.renboy94.fragmentsresponsive;

import  android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentLandscape extends Fragment{

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup  
container, @Nullable Bundle savedInstanceState) {
    return super.onCreateView(inflater, container, 
 savedInstanceState);
  }
}

FragmentPortrait

package com.example.renboy94.fragmentsresponsive;

import  android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentPortrait extends Fragment{

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
    return inflater.inflate(R.layout.portrait_fragment, container,  
false);
   }

}

这是错误:

Information:Gradle tasks [:app:assembleDebug] 
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72220Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42220Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJava   

/home/renboy94/AndroidStudioProjects/FragmentsResponsive/app/src/main/java/com/example/renboy94/fragmentsresponsive/MainActivity.java
Error:(26, 66) error: incompatible types: FragmentLandscape cannot be converted to Fragment
Error:(30, 65) error: incompatible types: FragmentPortrait cannot be converted to Fragment
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 7.22 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console

2 个答案:

答案 0 :(得分:1)

您的班级FragmentLandscape必须延长Fragment

如果您的应用程序的最低API级别为11,您可以使用:

android.app.Fragment

如果您希望支持向后兼容性,请使用:

android.support.v4.app.Fragment

如果您想在项目中使用支持库:

编辑build.gradle文件并添加依赖项

defaultConfig {
    minSdkVersion 08
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
}

buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:21.+'
}

答案 1 :(得分:0)

这听起来像是一个导入问题。确保您的片段正在扩展Fragment类,并确保导入显示android.support.v4.app.Fragment

import android.support.v4.app.Fragment;

public class NowPlayingFragment extends Fragment {

}