简体   繁体   中英

Fragment inflater says cannot resolve symbol

I am trying to create a blank fragment. But as soon as i create a fragment by right clicking on my package and choosing Fragment>Blank Fragment and create, the fragment inflater in the Fragment.java turns red on the Fragment Layout name and says "cannot resolve symbol". What am i doing wrong?

在此处输入图像描述

here is the code..

GroupsFragment.java

package com.passengerearth.chatme;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class GroupsFragment extends Fragment {

    public GroupsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_groups, container, false);
    }

}

fragment_groups.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GroupsFragment">

    <!-- TODO: Update blank fragment layout -->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Groups Fragment"
        android:layout_centerInParent="true"
        android:textSize="26sp"
        android:textStyle="bold" />

</RelativeLayout>

As per your comment:

I did that its giving me this error Default interface methods are only supported starting with Android N (--min-api 24): void androidx.lifecycle.DefaultLifecycleObserver.onCreate(androidx.lifecycle.LifecycleOwner)

You need to compile the project with Java 8. Open your build.gradle at module level, then:

android {
  compileOptions {
     sourceCompatibility JavaVersion.VERSION_1_8
     targetCompatibility JavaVersion.VERSION_1_8
  }
}

Sync and rebuild your project. The error should disappear after that.

Usually when you get that error R is not correctly imported. Also looking at your fragment code i don't see any R import, so you can try with:

import your.package.name.R

in your specific case should be:

 import com.passengerearth.chatme.R

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM