I
I
Ilya Kolpakov2020-08-24 07:59:25
Java
Ilya Kolpakov, 2020-08-24 07:59:25

How to rewrite this code in Kotlin?

Now I'm going through the book Android Programming The Big Nerd Ranch Guide (Android: Programming for Professionals 3rd edition). In the book, the code is given in Java, but I teach Kotlin and rewrite the examples in it. There was a problem with the Fragments theme.
(Code from the book)

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


Rewriting to Kotlin got the following:
override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View{
        val v: View = inflater.inflate(R.layout.fragment_crime, container, false)
        return v
    }


But R.layout.fragment_crime (the first argument in inflater) is highlighted in red. What could be the reason? There is a fragment_crime.xml file in the res/layout folder, but why can't it be transferred?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alekseyHunter, 2020-08-24
@kivinus1

R.layout.fragment_crime

1) Import not android.R but com.your_package.R
2) Click File->Invalidate/Restart

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question