我跟着一個關於多個佈局片段的教程,但我得到一個錯誤,我不知道它是什麼。我搜索有關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
如果導入問題沒有解決它,請張貼您的片段類以獲得更多幫助。 – doubleA
'我已經搜索了關於v4的東西'**爲什麼**?你沒有在代碼中使用'support library' Fragments。 –