错误:不兼容的类型:SearchFragment无法转换为Fragment

问题描述 投票:0回答:1

当我对项目进行一些更改并开始使用底部导航活动后,我使用导航抽屉活动时,此代码运行正常,它只是无法正常工作,并给了我不兼容类型的错误..我尝试了与此问题相关的所有可能答案但是我得到了同样的错误


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

public class HomeActivity extends AppCompatActivity  {

private TextView mTextMessage;

 SearchFragment searchFragment;
private FrameLayout Eframe;
private HomeFragment homeFragment;

private BottomNavigationView eNav;


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

    Eframe = findViewById(R.id.main_frame);

    searchFragment = new SearchFragment();
setFragment(searchFragment);
    eNav = findViewById(R.id.Nav);


    eNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            switch (menuItem.getItemId()) {
                case R.id.Search:
        >            setFragment(searchFragment);
                    return true;
                case R.id.Home:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.Profile:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;

                case R.id.Notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
                case R.id.Menu:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;

                default:
                    return false;
            }
        }
    });



    }




private void setFragment(Fragment fragment){
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_frame, fragment);
    fragmentTransaction.commit();

}

}

android-fragments android-fragmentactivity
1个回答
0
投票

而不是扩展AppCompatActivity扩展片段

© www.soinside.com 2019 - 2024. All rights reserved.