java – android spinner NullPointerException
•
Java
Hey guys, I'm in trouble with a spinner that throws nullpointerexpression. Am I sure I missed something stupid?
Thanks for your help
This is my code:
Public class main extends activity {/ * * called the first time an activity is created. */
public TextView strCurrency; public TextView strCurrencyOUT; Button butCalc; private Spinner spinner; private static final String[] array = { "AUD","CAD","USD","GBP",};
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); strCurrency = (TextView) this.findViewById(R.id.txtCurrency); strCurrencyOUT = (TextView) this.findViewById(R.id.txtOUT); butCalc = (Button) this.findViewById(R.id.butCalc); butCalc.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }}); Spinner spinner = (Spinner) findViewById(R.id.widget28); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,array); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter);
public void calculate() { String str = (String) spinner.getSelectedItem(); strCurrencyOUT.setText(str);
Logcat
W/KeyCharacterMap( 205): No keyboard for id 0 W/KeyCharacterMap( 205): Using default keymap: /system/usr/keychars/qwerty.kcm.bin D/AndroidRuntime( 205): Shutting down VM W/dalvikvm( 205): threadid=3: thread exiting with uncaught exception (group=0x4001aa28) E/AndroidRuntime( 205): Uncaught handler: thread main exiting due to uncaught exception E/AndroidRuntime( 205): java.lang.NullPointerException E/AndroidRuntime( 205): at biz.thorley.Currency.main.calculate(main.java:58) E/AndroidRuntime( 205): at biz.thorley.Currency.main$1.onClick(main.java:40) E/AndroidRuntime( 205): at android.view.View.performClick(View.java:2344) E/AndroidRuntime( 205): at android.view.View.onTouchEvent(View.java:4133) E/AndroidRuntime( 205): at android.widget.TextView.onTouchEvent(TextView.java:6510) E/AndroidRuntime( 205): at android.view.View.dispatchTouchEvent(View.java:3672) E/AndroidRuntime( 205): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) E/AndroidRuntime( 205): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) E/AndroidRuntime( 205): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) E/AndroidRuntime( 205): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) E/AndroidRuntime( 205): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712) E/AndroidRuntime( 205): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202) E/AndroidRuntime( 205): at android.app.Activity.dispatchTouchEvent(Activity.java:1987) E/AndroidRuntime( 205): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696) E/AndroidRuntime( 205): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658) E/AndroidRuntime( 205): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime( 205): at android.os.Looper.loop(Looper.java:123) E/AndroidRuntime( 205): at android.app.ActivityThread.main(ActivityThread.java:4203) E/AndroidRuntime( 205): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime( 205): at java.lang.reflect.Method.invoke(Method.java:521) E/AndroidRuntime( 205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) E/AndroidRuntime( 205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) E/AndroidRuntime( 205): at dalvik.system.NativeStart.main(Native Method) I/Process ( 53): Sending signal. PID: 205 SIG: 3 I/dalvikvm( 205): threadid=7: reacting to signal 3 E/dalvikvm( 205): Unable to open stack trace file '/data/anr/traces.txt': Permission denied I/Process ( 205): Sending signal. PID: 205 SIG: 9 I/ActivityManager( 53): Process biz.thorley.Currency (pid 205) has died. I/WindowManager( 53): WIN DEATH: Window{438d8e38 biz.thorley.Currency/biz.thorley.Currency.main paused=false} W/UsageStats( 53): Unexpected resume of com.android.launcher while already resumed in biz.thorley.Currency W/InputManagerService( 53): Got remoteexception sending setActive(false) notification to pid 205 uid 10023
Solution
This is the problem in your oncreate () method:
Spinner spinner = (Spinner) findViewById(R.id.widget28);
You assign the result to spinner, which is a local variable / reference main. The spinner class field is still null This is why you have a NullPointerException in the calculate () method
Replace the line above with:
this.spinner = (Spinner) findViewById(R.id.widget28);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码