6

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);
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
wolfgang
  • 93
  • 1
  • 1
  • 7
  • 1
    Welcome to stackoverflow. Please show us a snippet with your code, expected result and error. – Logain Nov 13 '17 at 19:19
  • Please try to avoid referencing external sources and create a minimal verifiable example https://stackoverflow.com/help/mcve – Logain Nov 13 '17 at 19:36

4 Answers4

11

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.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
1

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):

erafn
  • 11
  • 2
  • Don't do this: `val toolbar: Toolbar = toolbar` There's no point, you can already access `toolbar`. As a bonus Kotlin extensions already include caching of found views. – Eugen Pechanec Nov 13 '17 at 20:42
  • I changed @erafn line of val toolbar: Toolbar = view.findViewById(R.id.toolbar) as Toolbar to this below and it is not showing any red underlines. val toolbar: Toolbar = findViewById(R.id.toolbar) Also the first line in erafn answer worked perfectly. – wolfgang Nov 14 '17 at 06:18
  • `val toolbar: Toolbar` will not work because Activity *instantiation* happens way before `onCreate`. Since there is no view hierarchy before you call `setContentView`, `toolbar` will be null. And because the variable it of type `Toolbar` (non-nullable) your app will crash on start with a `KotlinNullPointerException`. – Eugen Pechanec Nov 14 '17 at 06:31
  • You're correct @Eugen Pechanec, That was a mistake and I've removed it – erafn Nov 14 '17 at 21:53
0

In my case, the problem was a invisible char due copy and paste from .PDF

Note that there are two chars between '{' and 'onClick' in the snippet below.

setOnClickListener { ​onClick(item) }

This is the cause of my nightmares.

  • 1
    I do not think that this is an appropriate answer. Imho, it's obvious that OP has not a copy/paste-problem (and also an accepted answer with no copy/paste problems at all). This should be a comment instead of an answer. – colidyre Aug 28 '18 at 22:58
  • Thanks @ferreiraiso ! I would lose lots of time trying to understand the reason if I didn't see your answer. – Oya Canli Aug 13 '19 at 15:25
  • fwiw, i had the same issue and this helped me, deleting some whitespace – sobelito Dec 31 '19 at 16:58
0

The main problem is the syntax, Whenever upgrade 3 series to 4. Java syntax transform to Kotlin syntax.

Kotlin Syntax:

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

Java Syntax:

@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2); // Inflate view hierarchy.
   }
   }

If you want to change Kotlin syntax to Java Syntax.

Came back to mainActivity programming panel after that TOOLS -> KOTLIN -> Show KOTLIN Bytecodes Then Check on the JVM 8 target and Tick that.

After that invalid catch/Restart

Problem has been resolved.