当我单击 "TRANSLATE" 按钮时,"App name" 一直停止。
"App name" keeps stopping when I click the "TRANSLATE" button.
所以我尝试使用 EditText 获取用户输入,并在单击 "translate" 按钮后将文本显示到 TextView 中。下面是我的 class 包含源代码。请帮忙!在使用 Google Cloud API 提取要翻译的内容之前,我需要能够成功提取用户的输入,这就是为什么我现在只是尝试通过 TextView 提取和显示它,但是它现在似乎不起作用...
public class Translator extends Fragment {
public Spinner spinner1, spinner2;
TextView textView;
EditText texttotranslate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.translator, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
ImageButton switchbutton;
Button translatebutton;
//Both Spinner's Languages
String [] Languages =
{"English","Malay","Chinese","Korean","Japanese","Arabic","Indonesian","Spanish"};
//LanguageFromSpinner
spinner1 = (Spinner) view.findViewById(R.id.languagefromspinner);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, Languages);
adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner1.setAdapter(adapter1);
//LanguageToSpinner
spinner2 = (Spinner) view.findViewById(R.id.languagetospinner);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, Languages);
adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner2.setAdapter(adapter2);
//Button to switch language between spinner1 & spinner2
switchbutton = (ImageButton) view.findViewById(R.id.languageswitchButton);
switchbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int spinner1Index = spinner1.getSelectedItemPosition();
spinner1.setSelection(spinner2.getSelectedItemPosition());
spinner2.setSelection(spinner1Index );
}
});
//Button to translate
translatebutton = (Button) view.findViewById(R.id.translatebutton);
translatebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView = (TextView) getView().findViewById(R.id.translatedtext);
texttotranslate = (EditText) v.findViewById(R.id.translatetextinput);
String textstorage = texttotranslate.getText().toString();
textView.setText(textstorage);
}
});
--------- beginning of crash
09-26 10:43:59.824 6294-6294/project.myapplication E/AndroidRuntime: FATAL
EXCEPTION: main
Process: project.myapplication, PID: 6294
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at project.myapplication.Translator.onClick(Translator.java:90)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
堆栈跟踪表明,当您尝试调用它的 getText()
方法时,您的 EditText
是 null
。
在您的事件处理程序中,您调用:
texttotranslate = (EditText) v.findViewById(R.id.translatetextinput);
在此上下文中,v
是触发 click
事件的视图,也就是按钮。您正在尝试在您的按钮中 中找到 ID 为 translatetextinput
.
的视图
您可能打算写:
texttotranslate = (EditText) getView().findViewById(R.id.translatetextinput);
它将在整个片段的视图层次结构中查找视图。
替换这些:
textView = (TextView) getView().findViewById(R.id.translatedtext);
texttotranslate = (EditText) v.findViewById(R.id.translatetextinput);
与:
textView = (TextView) view.findViewById(R.id.translatedtext);
texttotranslate = (EditText) view.findViewById(R.id.translatetextinput);
所以我尝试使用 EditText 获取用户输入,并在单击 "translate" 按钮后将文本显示到 TextView 中。下面是我的 class 包含源代码。请帮忙!在使用 Google Cloud API 提取要翻译的内容之前,我需要能够成功提取用户的输入,这就是为什么我现在只是尝试通过 TextView 提取和显示它,但是它现在似乎不起作用...
public class Translator extends Fragment {
public Spinner spinner1, spinner2;
TextView textView;
EditText texttotranslate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.translator, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
ImageButton switchbutton;
Button translatebutton;
//Both Spinner's Languages
String [] Languages =
{"English","Malay","Chinese","Korean","Japanese","Arabic","Indonesian","Spanish"};
//LanguageFromSpinner
spinner1 = (Spinner) view.findViewById(R.id.languagefromspinner);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, Languages);
adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner1.setAdapter(adapter1);
//LanguageToSpinner
spinner2 = (Spinner) view.findViewById(R.id.languagetospinner);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, Languages);
adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner2.setAdapter(adapter2);
//Button to switch language between spinner1 & spinner2
switchbutton = (ImageButton) view.findViewById(R.id.languageswitchButton);
switchbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int spinner1Index = spinner1.getSelectedItemPosition();
spinner1.setSelection(spinner2.getSelectedItemPosition());
spinner2.setSelection(spinner1Index );
}
});
//Button to translate
translatebutton = (Button) view.findViewById(R.id.translatebutton);
translatebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView = (TextView) getView().findViewById(R.id.translatedtext);
texttotranslate = (EditText) v.findViewById(R.id.translatetextinput);
String textstorage = texttotranslate.getText().toString();
textView.setText(textstorage);
}
});
--------- beginning of crash
09-26 10:43:59.824 6294-6294/project.myapplication E/AndroidRuntime: FATAL
EXCEPTION: main
Process: project.myapplication, PID: 6294
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at project.myapplication.Translator.onClick(Translator.java:90)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
堆栈跟踪表明,当您尝试调用它的 getText()
方法时,您的 EditText
是 null
。
在您的事件处理程序中,您调用:
texttotranslate = (EditText) v.findViewById(R.id.translatetextinput);
在此上下文中,v
是触发 click
事件的视图,也就是按钮。您正在尝试在您的按钮中 中找到 ID 为 translatetextinput
.
您可能打算写:
texttotranslate = (EditText) getView().findViewById(R.id.translatetextinput);
它将在整个片段的视图层次结构中查找视图。
替换这些:
textView = (TextView) getView().findViewById(R.id.translatedtext);
texttotranslate = (EditText) v.findViewById(R.id.translatetextinput);
与:
textView = (TextView) view.findViewById(R.id.translatedtext);
texttotranslate = (EditText) view.findViewById(R.id.translatetextinput);