skip to Main Content

I’m trying to make an android project where the user enters their name into an editText box and when a button is clicked, the name is printed out on a textView box. But, there is an error in my code: Expecting an element. The same error is there for 5 different lines of code:

1.TextView textview = findViewById(R.id.textView);
2.EditText inputText = findViewById(R.id.editText);
3.Button toastbutton = findViewById(R.id.button3);
4.toastbutton.setOnClickListener(new View.OnClickListener(){
5.public void onClick(View v){

I double checked the code and I rebuilt it but, still no result.

The whole code: “`

package com.example.buttontoast

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {


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

        TextView textview = findViewById(R.id.textView);
        EditText inputText = findViewById(R.id.editText);

       Button toastbutton = findViewById(R.id.button3);
        toastbutton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                TextView.setText("hi" + inputText.getText());
            }
    }


}

}

2

Answers


  1. Replace: TextView.setText("hi" + inputText.getText()); to textview.setText("hi" + inputText.getText());

    Login or Signup to reply.
  2. Your code has one missing import R.

    It looks like this

    import com.example.R;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search