0

i've tried to get user's input but i had this error for some reason :

 String firstNumber = nounu.getText().toString();
                              ^

symbol: method getText() location: variable nounu of type View

cannot find symbol method getText()

the code that I have :

    package com.mihai.stiri;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    View nounu;
    View nodoi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nounu = findViewById(R.id.nounu);
        nodoi = findViewById(R.id.nodoi);
    }

    public void showToastMessage(View view){
        String firstNumber = nounu.getText().toString();
        String secondNumber = nodoi.getText().toString();
        Toast.makeText( MainActivity.this,"Te-ai Inregistrat cu succes !"+firstNumber+"--"+secondNumber, Toast.LENGTH_SHORT ).show();
    }
}

I'm new to this android studio so I try to learn ..

  • 3
    Change your `nounu` type from `View` to `EditText` like: `EditText nounu` – Jatin Jan 10 '21 at 10:33
  • 2
    Why do you believe `View` has a method named `getText()`? The [documentation](https://developer.android.com/reference/android/view/View) doesn't list such a method. You did **read the documentation**, right? – Andreas Jan 10 '21 at 10:34

2 Answers2

0

You can't use getText with a View object as it's not a member function of the class View, you need to cast your view as an EditText, like follows:

package com.mihai.stiri;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText nounu;
    EditText nodoi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nounu = (EditText) findViewById(R.id.nounu);
        nodoi = (EditText) findViewById(R.id.nodoi);
    }

    public void showToastMessage(View view){
        String firstNumber = nounu.getText().toString();
        String secondNumber = nodoi.getText().toString();
        Toast.makeText( MainActivity.this,"Te-ai Inregistrat cu succes !"+firstNumber+"--"+secondNumber, Toast.LENGTH_SHORT ).show();
    }
}
mhdwajeeh.95
  • 417
  • 5
  • 13
0

You cannot methods like getText() while working with normal View. It can only be used with views such as TextView, EditText, etc.

For instance: This will work

TextView userName = findViewById(R.id.userName);
String strUserName = userName.getText().toString();

But, in case of normal views there is no such method getText()