Jpark9061
Jpark9061

Reputation: 1110

Android: How do I get the string value from inputfield?

I'm very new to Android, and I've been stuck on trying to get the value string from a material inputField component for hours.

To summarize, I have a button that clicks and opens up a dialog, and on the dialog there is an inputField and button. The button will read out what the inputField says using TextToSpeech.

I am confident that this is how you get the string from the material component (source: http://charmndroid.blogspot.com/2015/06/how-to-use-textinputlayout-in-android.html)

Everything works except for the string value extraction from inputField. If I only comment that out, everything works. Including the speech method (I tried with a static string and it works).

Could you please help me figure this out?

The error I'm getting:

2020-10-23 01:00:56.610 12012-12012/com.example.mint_anroidapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mint_anroidapp, PID: 31408
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.view.View$DeclaredOnClickListener.onClick(View.java:5979)
        at android.view.View.performClick(View.java:7125)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.view.View$DeclaredOnClickListener.onClick(View.java:5974)
        at android.view.View.performClick(View.java:7125) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992) 
        at android.view.View.performClickInternal(View.java:7102) 
        at android.view.View.access$3500(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27336) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.EditText com.google.android.material.textfield.TextInputLayout.getEditText()' on a null object reference
        at com.example.mint_anroidapp.MainActivity.speak(MainActivity.java:49)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.view.View$DeclaredOnClickListener.onClick(View.java:5974) 
        at android.view.View.performClick(View.java:7125) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992) 
        at android.view.View.performClickInternal(View.java:7102) 
        at android.view.View.access$3500(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27336) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private TextToSpeech text_to_speech;
    private TextInputLayout one_voice;


    Dialog myDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        one_voice = (TextInputLayout) findViewById(R.id.command_one_voice);

        myDialog = new Dialog(this);

        text_to_speech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    text_to_speech.setLanguage(Locale.CANADA);
                }
            }
        });
    }

    public void speak(View v) {
        String to_speak = one_voice.getEditText().getText().toString();
        text_to_speech.speak(to_speak, TextToSpeech.QUEUE_FLUSH, null);
    }

    public void OneShowPopup(View v) {
        TextView text_close;
        Button btnFollow = (Button) myDialog.findViewById(R.id.one_save_button);
        myDialog.setContentView(R.layout.command_one);
        text_close =(TextView) myDialog.findViewById(R.id.text_close);
        text_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialog.dismiss();
            }
        });
        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        myDialog.show();
    }

command_one.xml

           <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/command_one_voice"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:hint="@string/text">

                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </com.google.android.material.textfield.TextInputLayout>

         <com.google.android.material.button.MaterialButton
                    android:id="@+id/one_play_button"
                    android:onClick="speak"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp"
                    android:text="@string/play"
                    style="?attr/borderlessButtonStyle" />

MainActivity.xml

<com.google.android.material.button.MaterialButton
            android:onClick="OneShowPopup"
            android:id="@+id/command_one"
            android:layout_width="140dp"
            android:layout_height="100dp"
            android:padding="5dp"
            app:icon="@drawable/checked"
            android:textColor="#ffffff"
            android:text="Title 1"
            android:layout_marginRight="30dp"
            app:backgroundTint="#2BC9AF"
            app:iconGravity="textStart" />

Upvotes: 0

Views: 67

Answers (2)

Priyanka
Priyanka

Reputation: 1875

You need to add id to <com.google.android.material.textfield.TextInputEditText then you can get text from it directly.

Add id to your xml code as below

 <com.google.android.material.textfield.TextInputEditText
     android:id="@+id/edt_text"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" />

Then add below lines to activity

 private TextInputEditText edtText;

In oncreate add below line

 edtText= (TextInputEditText) findViewById(R.id.edt_text);

Edit your method

 public void speak(View v) {
    String to_speak = edtText.getText().toString();
    text_to_speech.speak(to_speak, TextToSpeech.QUEUE_FLUSH, null);
}

 

Upvotes: 0

grig
grig

Reputation: 847

one_voice = (TextInputLayout) findViewById(R.id.command_one_voice);

You try to find a view in your activity layout (MainActivity.xml), but it is in the dialog layout (command_one.xml). You should do something like this:

 one_voice = (TextInputLayout) myDialog.findViewById(R.id.command_one_voice);

But only after you init this dialog.

Upvotes: 1

Related Questions