Type mismatch: inferred type is HomeFragment but Fragment was expected

0

I'm doing a NavigationView with its Fragments to change view and to do it I just create a method "setupFragment (fragment: Fragment)" with a parameter of type Fragment so that it can put all the fragments it creates, but after implementing it I get an error saying that I have to put a fragment type object and that generates me rarity since I'm putting a fragment.

* Activeness Menu:

package com.example.gonzalo.examenparcial

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.v4.app.Fragment
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.widget.Toolbar
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_menu.*


class MenuActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    lateinit var tb: Toolbar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_menu)

        tb = toolbar as Toolbar
        tb.title = "NATURALEZA"

        setupNavigation()

        setupFragment(HomeFragment())

        navigation.menu.getItem(0).isChecked = true
    }

    private fun setupNavigation() {
        val toogle = ActionBarDrawerToggle(this, drawer, tb, R.string.drawerAbierto, R.string.drawerCerrado)

        toogle.isDrawerIndicatorEnabled = true

        drawer.addDrawerListener(toogle)
        toogle.syncState()
        navigation.setNavigationItemSelectedListener(this)
    }

    private fun setupFragment(fragment: Fragment){
        supportFragmentManager.beginTransaction().replace(R.id.frameLayout, fragment).commit()
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.arbol -> setupFragment(ArbolFragment())
            R.id.planta_semilla -> setupFragment(PlantaFragment())
            R.id.grano_semilla -> setupFragment(SemillaFragment())
        }
        drawer.closeDrawer(GravityCompat.START)
        return true
    }

    override fun onBackPressed() {
        if(drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START)
        }

        /*else if(HomeFragment().isVisible || LlegadasFragment().isVisible || SalidasFragment().isVisible){
            MapsActivity()
        }*/

        else{
            super.onBackPressed()
        }
    }
}

* activity_menu.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"/>

        <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?attr/actionBarSize">

        </FrameLayout>

    </android.support.constraint.ConstraintLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        app:itemTextColor="#009688"
        app:menu="@menu/main_menu"
        app:headerLayout="@layout/nav_header"
        android:fitsSystemWindows="true"
        android:layout_gravity = "start"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

</android.support.v4.widget.DrawerLayout>

* Build.Gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.gonzalo.examenparcial"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
}

* The error that it generates:

D:\WorkSpaces\Examen_Parcial\app\src\main\java\com\example\gonzalo\examenparcial\MenuActivity.kt: (27, 23): Type mismatch: inferred type is HomeFragment but Fragment was expected

D:\WorkSpaces\Examen_Parcial\app\src\main\java\com\example\gonzalo\examenparcial\MenuActivity.kt: (48, 41): Type mismatch: inferred type is ArbolFragment but Fragment was expected

D:\WorkSpaces\Examen_Parcial\app\src\main\java\com\example\gonzalo\examenparcial\MenuActivity.kt: (49, 50): Type mismatch: inferred type is PlantaFragment but Fragment was expected

D:\WorkSpaces\Examen_Parcial\app\src\main\java\com\example\gonzalo\examenparcial\MenuActivity.kt: (50, 49): Type mismatch: inferred type is SemillaFragment but Fragment was expected

* More errors

Compilation error. See log for more details
    
asked by Gian Franco Alexis Poma Vidal 29.10.2018 в 08:41
source

1 answer

0

According to your error:

  

Type mismatch: inferred type is HomeFragment but Fragment was expected

The problem is that your class HomeFragment is not extending from Fragment

public class HomeFragmentextends Fragment {
   ...
   ...
}
    
answered by 29.10.2018 в 16:46