我只想从微调器获取所选项目,我想分配该变量。我在这里得到typecasting exception
。
try
{
int id1 = Integer.parseInt(sp.getSelectedItem().toString());//this line is wrong
man.setMid(id1);
man.setMname(name.getText().toString());
db.updatemanager(man);
Toast.makeText(ctx, "Now its updated",Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
logcat的
12-22 04:27:27.774: E/AndroidRuntime(1781): FATAL EXCEPTION: main
12-22 04:27:27.774: E/AndroidRuntime(1781): Process: com.draw.androidproject2, PID: 1781
12-22 04:27:27.774: E/AndroidRuntime(1781): java.lang.NullPointerException
12-22 04:27:27.774: E/AndroidRuntime(1781): at com.draw.androidproject2.DialogFragment.ManagerupdateFragment$1.onClick(ManagerupdateFragment.java:57)
12-22 04:27:27.774: E/AndroidRuntime(1781): at android.view.View.performClick(View.java:4438)
12-22 04:27:27.774: E/AndroidRuntime(1781): at android.view.View$PerformClick.run(View.java:18422)
12-22 04:27:27.774: E/AndroidRuntime(1781): at android.os.Handler.handleCallback(Handler.java:733)
12-22 04:27:27.774: E/AndroidRuntime(1781): at android.os.Handler.dispatchMessage(Handler.java:95)
12-22 04:27:27.774: E/AndroidRuntime(1781): at android.os.Looper.loop(Looper.java:136)
12-22 04:27:27.774: E/AndroidRuntime(1781): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-22 04:27:27.774: E/AndroidRuntime(1781): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 04:27:27.774: E/AndroidRuntime(1781): at java.lang.reflect.Method.invoke(Method.java:515)
12-22 04:27:27.774: E/AndroidRuntime(1781): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-22 04:27:27.774: E/AndroidRuntime(1781): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-22 04:27:27.774: E/AndroidRuntime(1781): at dalvik.system.NativeStart.main(Native Method)
12-22 04:27:29.804: I/Process(1781): Sending signal. PID: 1781 SIG: 9
如果我使用Log打印该值,那么我就会获得价值。
Log.i(sp.getSelectedItem().toString());
这是Spinner代码
db = new DatabaseConnection(getActivity());
List <Manager> mgr = new ArrayList<>() ;
mgr = db.getAll();
adapter = new UpdateAdapter(ctx, mgr);
sp.setAdapter(adapter);