android spinner NullPointerException

时间:2010-08-27 07:11:25

标签: java android sdk nullpointerexception

嘿伙计们,我遇到了一个抛出nullPointerExpression的spiner的麻烦我确定我错过了一些愚蠢的东西?

谢谢你的帮助

这是我的代码:

public class main extends Activity {     /** 在第一次创建活动时调用。 * /

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

2 个答案:

答案 0 :(得分:7)

以下是oncreate()方法中存在的问题:

Spinner spinner = (Spinner) findViewById(R.id.widget28);

您可以在此处将结果分配给spinner,这是一个局部变量/引用。 main.spinner类字段仍为null。这就是NullPointerException方法中calculate()的原因。

将以上行替换为:

this.spinner = (Spinner) findViewById(R.id.widget28);

答案 1 :(得分:0)

在深入研究代码之前:它与“数组”中的备用逗号没有任何关系,是吗?

请发布StackTrace。