英文:
Android: How do I get the string value from inputfield?
问题
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();
}
}
<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" />
<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" />
英文:
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" />
答案1
得分: 1
one_voice =(TextInputLayout)findViewById(R.id.command_one_voice);
您试图在您的活动布局(MainActivity.xml
)中查找视图,但它位于对话框布局(command_one.xml
)中。您应该像这样做:
one_voice =(TextInputLayout)myDialog.findViewById(R.id.command_one_voice);
但是仅在您初始化此对话框之后。
英文:
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.
答案2
得分: 0
你需要在 <com.google.android.material.textfield.TextInputEditText 中添加 id,然后就可以直接从中获取文本。
在你的 XML 代码中添加 id,如下所示:
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edt_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
然后在活动中添加以下行:
private TextInputEditText edtText;
在 onCreate
方法中添加以下行:
edtText = (TextInputEditText) findViewById(R.id.edt_text);
修改你的方法:
public void speak(View v) {
String to_speak = edtText.getText().toString();
text_to_speech.speak(to_speak, TextToSpeech.QUEUE_FLUSH, null);
}
英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论