【问题标题】:Android Studio Error "Cannot resolve symbol attachToRoot"Android Studio 错误“无法解析符号 attachToRoot”
【发布时间】:2020-10-15 04:02:21
【问题描述】:

我正在尝试了解 Fragment 在 Android Studio 中的工作原理。 我在我的代码中设置了一个 LayoutInflater 但它说我的attachToRoot: false 无法解析。 这是我的代码

package com.example.rouletteapptest;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 */
public class fragment1 extends Fragment {

    TextView text,vers;

    public fragment1() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment1, container, attachToRoot:False);
        text = view.findViewById(R.id.AndroidApp);
        vers = view.findViewById(R.id.Description)
        return view;
    }
    public void change(String txt, String txt1){
        text.setText(txt);
        vers.setText(txt1);
    }
}

【问题讨论】:

  • 删除attachToRoot

标签: android android-studio android-layout android-fragments


【解决方案1】:

您应该将 attachToRoot:False 更改为 false,如下面的代码

View view = inflater.inflate(R.layout.fragment_fragment1, container, false);

我认为,你应该重构你的类名,因为不推荐这样做。 就像从“fragment1”变成“FragmentOne”

【讨论】:

    【解决方案2】:

    改变

    View view = inflater.inflate(R.layout.fragment_fragment1, container, attachToRoot:False);

    View view = inflater.inflate(R.layout.fragment_fragment1, container, false);

    【讨论】:

      【解决方案3】:

      只需输入False,然后按住SPACE就可以了。

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 2015-03-03
      • 2015-03-03
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多