我遇到的问题是使用onItemSelected方法。每当我尝试更改视图值时,示例:TextView
,EditView
都会引发错误。
我想要做的是当我点击微调器上的某些内容时,它会在文本上显示某些内容(当前任何内容)。希望你能帮忙!谢谢。
这是我的代码,您可以查看它:
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
答案 0 :(得分:1)
您没有引用TextView parm1,要获取引用,您必须首先调用findViewById,如下所示:
parm1 = (TextView) findViewById(R.id.yourId);
在你这样做之前,你可以使用那个textView,当比较字符串时不要使用==代替去Object.equal(),如果你有一个文字字符串,就像一个小的有用的提示而不是做
paths[index].equals("Rectangle")
DO
"Rectangle".equals(paths[index])
这样你就可以对空指针异常进行额外的验证,因为你的文字永远不会为null,但是“paths [index]”可能为null
希望它有帮助!
问候!