【Android studio】程序不报错但是停止运行

按照网上的视频例子在敲,但是不知道问题出在哪里,基本没有基础可言,麻烦大神们给看看,万分感谢

package com.example.pet;

import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import com.example.pet.base.BaseFragment;
import com.example.pet.collect.fragment.CollectFragment;
import com.example.pet.community.fragment.CommunityFragment;
import com.example.pet.home.fragment.HomeFragment;
import com.example.pet.type.fragment.TypeFragment;
import com.example.pet.user.fragment.UserFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends FragmentActivity {

    @BindView(R.id.frameLayout)
    FrameLayout frameLayout;
    @BindView(R.id.rg_main)
    RadioGroup rgMain;

    //装多个Fragment的实例集合
    private ArrayList<BaseFragment> fragments;

    //选中的Fragment的对应的位置
    private int position;

    //缓存的上次显示的Fragment
    private Fragment tempFragemnt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //ButterKnife和当前Activity绑定,绑定后自动实例化
        ButterKnife.bind(this);

        //初始化Fragment
        initFragment();
        //设置RadioGroup的监听
        initListener();

    }



    private void initListener() {

        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home://主页
                        position = 0;
                        break;
                    case R.id.rb_type://分类
                        position = 1;
                        break;
                    case R.id.rb_community://发现
                        position = 2;
                        break;
                    case R.id.rb_collect://收藏
                        position = 3;
                        break;
                    case R.id.rb_user://个人中心
                        position = 4;
                        break;
                    default:
                        position = 0;
                        break;
                }

                //根据位置去取不同的Fragment
                BaseFragment baseFragment = getFragment(position);
                //第一个参数:上次显示的Fragment,第二个参数:当前正要显示的Fragment
                switchFragment(tempFragemnt, baseFragment);
            }
        });
        //默认选中主页面
        rgMain.check(R.id.rb_home);
    }

    //切换Fragment,隐藏上一个,显示当前
    private void switchFragment(Fragment fromFragment, BaseFragment nextFragment) {
        if (tempFragemnt != nextFragment) {
            tempFragemnt = nextFragment;
            if (nextFragment != null) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                // 判断 nextFragment 是否添加
                if (!nextFragment.isAdded()) {
                    // 隐藏当前 Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    //添加Fragment
                    transaction.add(R.id.frameLayout, nextFragment).commit();
                } else {
                    // 隐藏当前 Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.show(nextFragment).commit();
                }
            }
        }
    }
    private BaseFragment getFragment(int position) {
        if (fragments != null && fragments.size() > 0) {
            BaseFragment baseFragment = fragments.get(position);
            return baseFragment;
        }
        return null;
    }

    //添加的时候要按照顺序
    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new TypeFragment());
        fragments.add(new CommunityFragment());
        fragments.add(new CollectFragment());
        fragments.add(new UserFragment());
    }

}

附上logcat

图片说明图片说明

1个回答

错误出在你的HomeFragment里和这里没关系,要学会看日志啊,图片说明直接找你的包名应该很快就可以看到错误出在哪里,现在工具也标识的很清楚都用蓝色帮你区分了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐