片段onCreateView未被访问

时间:2020-10-26 19:03:01

标签: java android mobile fragment

这是我的片段代码,无法到达onCreateView。我该如何访问?我也收到此错误: java.lang.ClassCastException:androidx.appcompat.widget.AppCompatTextView无法转换为android.view.ViewGroup

我还附加了fragment_profile.xml供参考。


public class ProfileFragment extends Fragment {
    private EditText etName;
    private EditText etEmail;
    private EditText etPassword;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "hello");


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                             Bundle savedInstanceState) {

      

        View view = inflater.inflate(R.layout.fragment_profile,parent,false);
        etName = (EditText) view.findViewById(R.id.et_edit_name);
        etEmail  = (EditText) view.findViewById(R.id.et_edit_email);
        etPassword = (EditText) view.findViewById(R.id.et_edit_password);
        return view;


    }





}

这是我的片段的xml文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/profile_page"
    android:layout_width="match_parent"
    android:layout_height="match_parent"


    android:background="#fff"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="600dp"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="50dp"
            android:layout_marginRight="30dp"
            android:background="#D0F6DB"
            android:elevation="4dp"
            android:orientation="vertical"
            android:padding="20dp">


            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="30dp">

                <TextView
                    android:id="@+id/tv_editdetails"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="25dp"
                    android:layout_marginLeft="25dp"
                    android:layout_marginTop="40dp"
                    android:layout_marginBottom="5dp"
                    android:fontFamily="sans-serif"
                    android:gravity="center_horizontal"
                    android:text="Profile Details"
                    android:textAlignment="center"
                    android:textColor="#070707"
                    android:textSize="40sp" />


                <EditText
                    android:id="@+id/et_edit_email"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:background="@drawable/et_custom"
                    android:fontFamily="sans-serif"
                    android:hint="@string/e_mail"
                    android:inputType="textEmailAddress"
                    android:padding="15dp"
                    android:textSize="15sp" />


                <EditText
                    android:id="@+id/et_edit_name"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="20dp"
                    android:background="@drawable/et_custom"
                    android:fontFamily="sans-serif"
                    android:hint="@string/your_name"
                    android:inputType="textPersonName"
                    android:padding="15dp"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/et_edit_password"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="20dp"
                    android:background="@drawable/et_custom"
                    android:fontFamily="sans-serif"
                    android:hint="@string/password"
                    android:inputType="textPassword"
                    android:padding="15dp"
                    android:textSize="15sp" />


                <Button
                    android:id="@+id/save"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="110dp"
                    android:layout_marginTop="100sp"
                    android:background="@android:color/holo_red_dark"
                    android:text="Save"
                    android:textColor="@android:color/white"
                    android:textStyle="bold" />

            </LinearLayout>

        </RelativeLayout>


    </LinearLayout>
</LinearLayout>

我通过上一个片段对其进行调用的代码

 prof_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v("clicked","profile");
                Fragment fragment = new ProfileFragment();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.tv_account, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                

            }
        });

0 个答案:

没有答案
相关问题