Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Android Studio and Main Activity.kt expecting member declaration

I have been trying to learn through following YouTube tutorials. I am using Android Studio 3.1 Canary and I get to the same point in the tutorials and get stuck. For instance if you go to this YouTube tutorial https://youtu.be/3RMboPhUbmg?t=210 at the 3:30 min mark.

When they are inputting the MaterialSearchView searchView; it shows up for me with a red underline saying "expecting member declaration" and no matter how many searches I try I cannot find an answer. What is the solution to this error? Thanks

This is the code contained in the Main2Activity.kt. The result should be calling or knowing the toolbar and materialsearchview objects

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.Toolbar
import com.miguelcatalan.materialsearchview.MaterialSearchView
import kotlinx.android.synthetic.main.activity_main2.*

class Main2Activity : AppCompatActivity () {
    **MaterialSearchView searchView;**  "expecting member declaration error"
    **Toolbar  toolbar;** "expecting member declaration error"
    Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        toolbar=(Toolbar()) findViewbyId(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
    *private void searchViewCode()
    {
        searchView=(MaterialSearchView)findViewById(R.id.search_view);
    }
}
like image 862
wolfgang Avatar asked Nov 13 '17 18:11

wolfgang


2 Answers

1) Understand your language syntax

Your tutorial is in Java. You try to write in Kotlin. Java and Kotlin have different syntax and if you reproduce this tutorial word for word in Kotlin, it will fail.

Follow the turorial and write your code in Java. Switch to Kotlin later when you're more confident with what you're doing. Focus on one thing at a time.

2) Find views at the right time

The Activity object is instatiated for you by the framework with a public empty constructor. At this time there are no views to be found. If you call findViewById any time before setContentView it will return null and crash if you try to assign it to a non-nullable variable.

The above applies for both Java and Kotlin.

For Java follow the tutorial. It should look something like this:

Toolbar toolbar; // Declare the variable here so it's accessible outside of onCreate.

@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2); // Inflate view hierarchy.
    toolbar = (Toolbar) view.findViewById(R.id.toolbar); // Find your views.
    setSupportActionBar(toolbar);
}

In Kotlin there are several options.

You can use lateinit modifier which allows you to declare a non-nullable variable but assign it later in onCreate.

lateinit var toolbar: Toolbar

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main2)
    toolbar = findViewById(R.id.toolbar) as Toolbar
    setSupportActionBar(toolbar)
}

Or you can use lazy delegate. The variable will be assigned when you first access it.

val toolbar: Toolbar by lazy(LayzThreadSafetyMode.NONE) {
    toolbar = findViewById(R.id.toolbar) as Toolbar
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main2)
    setSupportActionBar(toolbar)
}

Don't use the delegate. It creates an unnecessary holder object for each lazy, that's wasteful.

You can also use Kotlin Android Extensions and just access toolbar directly because all of the heavy lifting is done for you.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main2)
    setSupportActionBar(toolbar)
}

Alternatively you can use View Binding available since Android Studio 3.6 Canary 11.

private lateinit var binding: ActivityMain2Binding

@Override
fun onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
    binding = ActivityMain2Binding.inflate(layoutInflater)
    setContentView(binding.root)
    setSupportActionBar(binding.toolbar)
}

You get high performance of the first option and strong type safety on top.

like image 164
Eugen Pechanec Avatar answered Nov 02 '22 11:11

Eugen Pechanec


It seems you are declaring your searchView and toolbar variables using Java syntax, and not the Kotlin syntax, so the compiler does not understand what you are declaring.

So change the declaration to:

var searchView: MaterialSearchView? = null
val toolbar: Toolbar = view.findViewById(R.id.toolbar) as Toolbar 

or if you are using Kotlin Android extensions you should be able to retrieve the toolbar like so (using the view id directly):

like image 35
erafn Avatar answered Nov 02 '22 11:11

erafn