我跟着一个关于多个布局片段的教程,但我得到一个错误,我不知道它是什么。我搜索有关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。 –