gpt4 book ai didi

Java Android Spinner onItemSelected

转载 作者:行者123 更新时间:2023-12-01 13:27:28 25 4
gpt4 key购买 nike

我遇到的问题是 onItemSelected 方法。每当我尝试更改 View 值(例如:TextViewEditView)时,它都会引发错误。

我想做的是,当我单击微调器上的某些内容时,它会在文本上显示某些内容(当前的任何内容)。我希望你能帮忙!谢谢。

这是我的代码,您可以查看它:

public class MainActivity extends Activity implements OnItemSelectedListener {

Spinner spinner;
String[] paths = {"Rectangle", "Circle", "Triangle"};
String selected;
Button equals;
TextView parm1, parm2;
EditText value1, value2;
CheckBox checkArea, checkPerm;
Boolean checkedArea = true, checkedPerm = false;

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

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, paths);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int index = arg0.getSelectedItemPosition();

if(paths[index] == "Rectangle"){
//Does work
Toast.makeText(getBaseContext(), "You selected Rectangle", Toast.LENGTH_LONG).show();

//Doesn't work
parm1.setText("Hello");
}
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}

这是我运行程序时弹出的错误。虚拟设备将一直运行该程序,直到我打开它,然后它会说不幸的是*“Programs_name”*已停止工作。这是错误日志:

 02-12 17:26:41.395: D/dalvikvm(1478): GC_FOR_ALLOC freed 45K, 4% free 3331K/3444K, paused 47ms, total 51ms
02-12 17:26:41.985: D/AndroidRuntime(1478): Shutting down VM
02-12 17:26:41.985: W/dalvikvm(1478): threadid=1: thread exiting with uncaught exception (group=0xb1aadba8)
02-12 17:26:41.995: E/AndroidRuntime(1478): FATAL EXCEPTION: main
02-12 17:26:41.995: E/AndroidRuntime(1478): Process: com.example.gcfcalculator, PID: 1478
02-12 17:26:41.995: E/AndroidRuntime(1478): java.lang.NullPointerException
02-12 17:26:41.995: E/AndroidRuntime(1478): at com.example.gcfcalculator.MainActivity.onItemSelected(MainActivity.java:53)
02-12 17:26:41.995: E/AndroidRuntime(1478): at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
02-12 17:26:41.995: E/AndroidRuntime(1478): at android.widget.AdapterView.access$200(AdapterView.java:48)
02-12 17:26:41.995: E/AndroidRuntime(1478): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
02-12 17:26:41.995: E/AndroidRuntime(1478): at android.os.Handler.handleCallback(Handler.java:733)
02-12 17:26:41.995: E/AndroidRuntime(1478): at android.os.Handler.dispatchMessage(Handler.java:95)
02-12 17:26:41.995: E/AndroidRuntime(1478): at android.os.Looper.loop(Looper.java:136)
02-12 17:26:41.995: E/AndroidRuntime(1478): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-12 17:26:41.995: E/AndroidRuntime(1478): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 17:26:41.995: E/AndroidRuntime(1478): at java.lang.reflect.Method.invoke(Method.java:515)
02-12 17:26:41.995: E/AndroidRuntime(1478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-12 17:26:41.995: E/AndroidRuntime(1478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-12 17:26:41.995: E/AndroidRuntime(1478): at dalvik.system.NativeStart.main(Native Method)
02-12 17:26:47.055: I/Process(1478): Sending signal. PID: 1478 SIG: 9

最佳答案

您没有对 TextView parm1 的引用,要获取引用,您必须首先调用 findViewById,如下所示:

parm1 = (TextView) findViewById(R.id.yourId);

在你这样做之前,你将能够使用该textView,而且,在比较字符串时,不要使用==,而是使用Object.equal(),如果你有一个文字字符串,这只是一个有用的小提示而不是做

paths[index].equals("Rectangle")

"Rectangle".equals(paths[index])

这样你就可以对空指针异常进行额外的验证,因为你的文字永远不可能为空,但是“paths[index]”有可能为空

希望有帮助!

问候!

关于Java Android Spinner onItemSelected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21741418/

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