Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法动态添加片段_Android - Fatal编程技术网

Android 无法动态添加片段

Android 无法动态添加片段,android,Android,我尝试过动态添加片段,但每次我切换到动态添加片段而不是静态添加片段时,应用程序都会停止 package com.hiro.empty; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; public class Ma

我尝试过动态添加片段,但每次我切换到动态添加片段而不是静态添加片段时,应用程序都会停止

package com.hiro.empty;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Fragment fragment = new MyFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.container, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}
Logcat输出:

2019-10-02 15:38:51.056 16022-16022/com.hiro.empty E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hiro.empty, PID: 16022
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:5106)
        at android.view.ViewGroup.addView(ViewGroup.java:4935)
        at android.view.ViewGroup.addView(ViewGroup.java:4875)
        at android.view.ViewGroup.addView(ViewGroup.java:4848)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:887)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        ...

MyFragment类

public class MyFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.fragment, container);
    }
}

关键是
inflate
方法中的第三个布尔参数。它会将膨胀视图附着到父视图。要修复错误,应将
onCreateView
方法更改为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.fragment, container, false);
}

避免将null作为父级传递以正确设置LayoutParams。

关键是
充气方法中的第三个布尔参数。它会将膨胀视图附着到父视图。要修复错误,应将
onCreateView
方法更改为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.fragment, container, false);
}

避免将null作为父项传递,以便正确设置LayoutParams。

请从logcat添加stacktrace和崩溃信息。@YuriiKyrylchuk我在logcat输出中添加了异常。您还可以添加MyFragment代码吗?尤其是onCreateView()method@YuriiKyrylchuk这是整个班级。我剥离了所有其他内容,因为我无法找出代码的错误。请从logcat添加一个stacktrace,其中包含崩溃信息。@YuriiKyrylchuk我在logcat输出中添加了异常。您还可以添加我的片段代码吗?尤其是onCreateView()method@YuriiKyrylchuk这是整个班级。我剥去了所有其他东西,因为我无法找出代码的错误。我认为可能的副本解决了这个问题。我没注意到,我想这解决了问题。我没注意到那个论点