2012-05-16 71 views
1

我有一個微調,其工作正常,當我通過微調在數據庫中插入值,但是當我從數據庫中獲取值並將索引傳遞給微調它也可以,但是當我點擊它時更改它給出的錯誤Java空指針異常的值。請幫助我。微調異常在點擊選擇

array_spinner=new String[4]; 
     array_spinner[0]= sing.gs.getApplicator1(); 
     array_spinner[1]= sing.gs.getApplicator2(); 
     array_spinner[2]= sing.gs.getApplicator3(); 
     array_spinner[3]= sing.gs.getApplicator4(); 
     app = (Spinner) findViewById(R.id.spnr_app); 

     ArrayAdapter adapter = new ArrayAdapter(this, 
     android.R.layout.simple_spinner_item, array_spinner); 
     app.setAdapter(adapter); 

     //singleton check 
     if (sing.savedRecord) 
      { 
       getSetting(); 
      } 

getSetting方法

private void getSetting() 
{ 
    try 
    { 

     app.setSelection(Integer.valueOf(sing.gs.getApplicator_name())); 
     datePicker.setText(sing.gs.getSpray_date().toString()); 

    } 
    catch (Exception e) 
    { 
     Log.e("Retrieve Error", e.toString()); 
     e.printStackTrace(); 
    } 
} 

logcat的

05-16 17:32:58.511: E/AndroidRuntime(910): FATAL EXCEPTION: main 
05-16 17:32:58.511: E/AndroidRuntime(910): java.lang.NullPointerException 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:332) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:328) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.AbsListView.obtainView(AbsListView.java:1409) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.ListView.onMeasure(ListView.java:1127) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.view.View.measure(View.java:8313) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.view.View.measure(View.java:8313) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 
05-16 17:32:58.511: E/AndroidRuntime(910): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.view.View.measure(View.java:8313) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.view.View.measure(View.java:8313) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.view.View.measure(View.java:8313) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.view.ViewRoot.performTraversals(ViewRoot.java:839) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.os.Looper.loop(Looper.java:123) 
05-16 17:32:58.511: E/AndroidRuntime(910): at android.app.ActivityThread.main(ActivityThread.java:3683) 
05-16 17:32:58.511: E/AndroidRuntime(910): at java.lang.reflect.Method.invokeNative(Native Method) 
05-16 17:32:58.511: E/AndroidRuntime(910): at java.lang.reflect.Method.invoke(Method.java:507) 
05-16 17:32:58.511: E/AndroidRuntime(910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
05-16 17:32:58.511: E/AndroidRuntime(910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
05-16 17:32:58.511: E/AndroidRuntime(910): at dalvik.system.NativeStart.main(Native Method) 
+0

後堆棧跟蹤和微調項點擊全碼。 – user370305

+0

我沒有做任何微調項目點擊,我使用它的默認行爲,因爲我用「.getSelectedItemPosition」來獲取數據,好吧我正在用logcat更新它 – Ishtiaq

+0

在這裏發佈完整的代碼並詳細解釋.. –

回答

1

請檢查它看起來,你的一個sing.gs.getApplicator1();值爲null。請檢查所有sing.gs.getApplicator ..........

原因: 去行無394(whcih是你的SDK ArrayAdepter 328)

https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java

394 - text.setText(item.toString());

所以這裏這兩個文本可能爲空或項目可能爲空。

,但使用的是內置的佈局,使文本不應爲空,因此項目可以爲空....

0

安置自己的完整的代碼和詳細的解釋,這裏是一個樣本微調希望將有助於你,

spinnerSize = (Spinner) findViewById(R.id.spinnerSize); 
    sizeArray = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_dropdown_item, size); 
    spinnerSize.setAdapter(sizeArray); 
    spinnerSize.setOnItemSelectedListener(new OnItemSelectedListener() { 

     public void onItemSelected(AdapterView<?> parent, View v, int pos, 
       long id) { 
      if (sizeCount != 0) { 
       Constants.enhancedImage.setTextSize(size[pos]); 
       receiver.setTextSize(Integer.parseInt(size[pos])); 
       sender.setTextSize(Integer.parseInt(size[pos])); 
       message.setTextSize(Integer.parseInt(size[pos])); 
      } 
      sizeCount++; 
     } 

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

和字符串大小[]是,

String[] size = { "10", "12", "14", "16", "18", "20", "22"};