Cannot resolve method ‘setContentView‘ in ‘ThreeFragment‘

报错原因

Cannot resolve method ‘setContentView’ in ‘ThreeFragment’ 错误的原因是 setContentView 是 Activity 中的方法,而你在 Fragment 中使用了它。Fragment 的布局加载方式与 Activity 不同。

解决方法

1. 使用 onCreateView 加载布局

在 Fragment 中,应该通过 onCreateView 方法加载布局,而不是 setContentView。

示例代码:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;

public class ThreeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 加载布局文件
        View view = inflater.inflate(R.layout.fragment_three, container, false); // 替换为你的布局文件名
        return view;
    }
}

inflater.inflate:用于加载布局文件。
container:父容器,通常是 Fragment 的根布局。
false:表示不将布局直接附加到父容器(由系统处理)。

2. 初始化视图

在 onCreateView 中加载布局后,可以通过 view.findViewById 初始化视图组件。

示例代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_three, container, false);

    // 初始化视图
    TextView textMemberOffer = view.findViewById(R.id.text_member_offer);
    TextView textExistingMember = view.findViewById(R.id.text_existing_member);

    // 设置点击事件
    textMemberOffer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "恭喜您,购买成功,您已经是会员了", Toast.LENGTH_SHORT).show();
        }
    });

    textExistingMember.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), AnotherActivity.class); // 替换为你的目标 Activity
            startActivity(intent);
        }
    });

    return view;
}

3. 确保布局文件正确

确保 fragment_three.xml 布局文件存在,并且 TextView 的 id 正确。

示例布局文件 (fragment_three.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_member_offer"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="35dp"
        android:background="@color/Black"
        android:gravity="center"
        android:text="万达广场会员专场只需99元!"
        android:textColor="@color/gold"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/text_existing_member"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:layout_marginTop="35dp"
        android:textSize="16sp"
        android:text="已有商城会员,直接观影" />
</LinearLayout>

4. 跳转到另一个 Activity

如果需要在 Fragment 中跳转到另一个 Activity,使用 getActivity() 获取上下文。

示例代码:

Intent intent = new Intent(getActivity(), AnotherActivity.class);
startActivity(intent);

总结

在 Fragment 中,使用 onCreateView 加载布局,而不是 setContentView。
通过 view.findViewById 初始化视图组件。
确保布局文件正确,并且 id 与代码中一致。

关注“鹿溪IT工作室”,后续我们会不定时分享新的bug修改意见,有时候不一定全对,欢迎大家留言批评指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值