2015-07-11 57 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 { 

}