Ich habe versucht, meinem Fragmentcontainer FrameLayout ein Fragment hinzuzufügen
Ich habe eine XML-Datei mit dem Namen editor.xml erstellt, die ein FrameLayout enthält. In meiner Hauptaktivität versuche ich, mein benutzerdefiniertes Fragment zu meinem FrameLayout hinzuzufügen.
Der Fehler, den ich beim Hinzufügen meines Fragments erhalte, ist:
Die Methode add (int, Fragment) vom Typ FragmentTransaction gilt nicht für die Argumente (int, editorFrag)
Mein editorFrag erweitert jedoch Fragment, sodass ich verwirrt bin, warum dies geschieht. Unten ist mein Code für die Dateien, die ich erwähnt habe. Jede Hilfe wird geschätzt.
Editor.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
editorFrag.java
public class editorFrag extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.newlevel, container, false);
}
}
MainActivity.java
public class editorActivity extends FragmentActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.editor);
// Check that the activity is using the layout version with the fragment_container FrameLayout
if(findViewById(R.id.fragment_container) != null)
{
// if we are being restored from a previous state, then we dont need to do anything and should
// return or else we could end up with overlapping fragments.
if(savedInstanceState != null)
return;
// Create an instance of editorFrag
editorFrag firstFrag = new editorFrag();
// add fragment to the fragment container layout
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFrag);
}
}
}
Antwortete:
Luksprog beantwortete dieses Problem für mich, indem er mich aufforderte, meine Importe zu überprüfen. Eclipse hat sich entschieden, die SDK-Version von Fragment anstelle der von mir benötigten Support-Version zu importieren. Danke für die Hilfe.