-2

I am new to kotlin I am adding bottom bar in my app

val bottomNavigationView = findViewById<View>(R.id.navigation) as BottomNavigationView
    BottomNavigationViewHelper.removeShiftMode(bottomNavigationView)
    bottomNavigationView.setOnNavigationItemSelectedListener { item ->
        var selectedFragment: Fragment? = null
        when (item.itemId) {
            R.id.action_item1 -> selectedFragment = ItemOneFragment.newInstance()
            R.id.action_item2 -> selectedFragment = ItemTwoFragment.newInstance()
            R.id.action_item3 -> selectedFragment = ItemThreeFragment.newInstance()
            R.id.action_item4 -> selectedFragment = ItemThreeFragment.newInstance()
        }
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.frame_layout, selectedFragment)
        transaction.commit()
        true
    }

    //Manually displaying the first fragment - one time only
    val transaction = supportFragmentManager.beginTransaction()
    transaction.replace(R.id.frame_layout, ItemOneFragment.newInstance())
    transaction.commit()

ItemOneFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
 import android.view.ViewGroup;
 public class ItemOneFragment extends Fragment {
 public static ItemOneFragment newInstance() {
    ItemOneFragment fragment = new ItemOneFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_item_one, container, false);
}}

But I am getting the error : type mismatch Required : Fragment at ItemOneFragment.newInstance() I have tried all stuff Thanks in advance.

Darshan
  • 38
  • 10

1 Answers1

1

It looks like you're importing android.app.Fragment in the file containing your bottomNavigationView code. ItemOneFragment.newInstance() returns android.support.v4.app.Fragment, which would be incompatible with android.app.Fragment.

Changing the import to android.support.v4.app.Fragment should solve the issue.

Christian Brüggemann
  • 2,152
  • 17
  • 23