gpt4 book ai didi

android - 当我单击 "App name"按钮时,"TRANSLATE"不断停止。

转载 作者:行者123 更新时间:2023-12-02 08:48:28 24 4
gpt4 key购买 nike

因此,我尝试使用 EditText 获取用户输入,并在单击“翻译”按钮后将文本显示到 TextView 中。下面是我的类,其中包含源代码。请帮忙!在使用 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$2.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() 方法时,您的 EditTextnull

在事件处理程序中,您调用:

texttotranslate = (EditText) v.findViewById(R.id.translatetextinput);

在此上下文中,v 是触发 click 事件(也称为按钮)的 View 。您正在尝试在按钮内部查找 ID 为 translatetextinput 的 View 。

您可能想写:

texttotranslate = (EditText) getView().findViewById(R.id.translatetextinput);

它将在整个 fragment 的 View 层次结构中查找 View 。

关于android - 当我单击 "App name"按钮时,"TRANSLATE"不断停止。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52516021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com