請問 Fragment 在add和hide時為甚麼報錯?

如題我在製作導覽列時,
add和hide不給我放我new的fragment,實在想不出原因,
請各位大大告訴我

package com.example.test;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

import com.example.test.home_Fragment;
import com.example.test.live_fragment;
import com.example.test.car_fragment;
import com.example.test.notify_fragment;
import com.example.test.set_fragment;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    private home_Fragment homef;
    private live_fragment livef;
    private car_fragment carf;
    private notify_fragment notifyf;
    private set_fragment setf;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_live:
                    mTextMessage.setText("live");
                    return true;
                case R.id.navigation_car:
                    mTextMessage.setText("car");
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText("notification");
                    return true;
                case R.id.navigation_setting:
                    mTextMessage.setText("setting");
                    return true;
            }
            return false;
        }
    };

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

        initial();

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    //初始化fragment
    private void initial()
    {
        homef = new home_Fragment();
        carf = new car_fragment();
        livef = new live_fragment();
        notifyf = new notify_fragment();
        setf = new set_fragment();

        FragmentTransaction itfragment = getFragmentManager().beginTransaction();
        itfragment.add(R.id.content,homef).add(R.id.content,carf).add(R.id.content,livef).add(R.id.content,notifyf)
                .add(R.id.content,setf);
        itfragment.hide(homef).hide(carf).hide(livef);//隐藏fragment
        itfragment.addToBackStack(null);
        itfragment.commit();
    }

}

图片说明

图片说明

1个回答

首先 add 的语法使用不正确,其次创建fragment对象的时候 申明 用Fragment

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