2015-07-11 53 views
-1

我跟着一个关于多个布局片段的教程,但我得到一个错误,我不知道它是什么。我搜索有关v4的东西,但我不明白,我不知道如何将它应用到我的代码“FragmentLandscape”无法转换为片段

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 
+0

如果导入问题没有解决它,请张贴您的片段类以获得更多帮助。 – doubleA

+0

'我已经搜索了关于v4的东西'**为什么**?你没有在代码中使用'support library' Fragments。 –

回答

1

您的类FragmentLandscape必须扩展Fragment

如果您的应用程序的最低API等级是11,你可以使用:

android.app.Fragment

如果你想支持向后兼容性,然后使用:

android.support.v4.app.Fragment

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

编辑您的build.gradle fi并添加依赖关系

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.+' 
} 
+0

非常感谢你! – rendell

0

这听起来像是一个进口问题。确保您的片段延长片段类,并确保导入说android.support.v4.app.Fragment

import android.support.v4.app.Fragment; 

public class NowPlayingFragment extends Fragment { 

}