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

时间:2019-10-30 02:01:09

标签: android-fragments android-fragmentactivity

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


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();

}

}

1 个答案:

答案 0 :(得分:0)

而不是扩展了AppCompatActivity 扩展片段