T'm versucht eine sehr einfache Sache. Fügen Sie ein Fragment über Java-Code hinzu. Aber es zeigt mir Fehler in (fragmentTransaction.add (R.id.fragment_container, Fragment);). Ich kann nicht verstehen, was hier falsch ist. Bitte helfen Sie. Es gibt zwei Klassen, die Hauptklasse und die Fragmentklasse. und zwei Layouts das Hauptlayout und das Fragmentlayout.Hinzufügen von Fragment zu Android
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.chetan.fragmentprtc.MainActivity">
<fragment
android:id="@+id/fragment_container"
android:name="android.app.DialogFragment"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
package com.example.chetan.fragmentprtc;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
BlankFragment fragment = new BlankFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.chetan.fragmentprtc.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
package com.example.chetan.fragmentprtc;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank,container,false);
return view;
}
}
Hallo Wizard! Ich habe den Code in der XLM-Datei geändert. Aber es gibt einen Fehler in Zeile: transaction.replace (R.id.fragment_container, Fragment); –