2015-03-25 60 views
2

我的工作中,我一直在使用ormlite後,我想在微調顯示這些信息,以從服務器獲取一些信息,將它們保存在數據庫中的項目,從數據庫機器人填補了微調

所以從數據庫到微調得到的信息我稱這種方法:

public Dao<ServiceProviderCompanies, Integer> getTowServiceProviderCompaniesDao() { 
     if (null == towServiceProviderCompaniesDao) { 
      try { 
       towServiceProviderCompaniesDao = getDao(ServiceProviderCompanies.class); 
      } catch (java.sql.SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
     return towServiceProviderCompaniesDao; 
    } 

我把它叫做在我的活動是這樣的:

String[] ServicePCompanies; 

try {List<ServiceProviderCompanies> mSerProCom=towManager.getTowServiceProviderCompaniesDao().queryForAll(); 
      ServicePCompanies=new String[mSerProCom.size()]; 
      for(int i=1;i<mSerProCom.size();i++){ 
       ServicePCompanies[i]=mSerProCom.get(i)._serviceRequesterServiceProviderCompany; 
      } 
      //CompaniesForSpinner=towManager.getTowServiceProviderCompaniesDao().queryForAll().toString(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 

,我充滿了我的SP內使用的代碼

Spinner entrAssistanceList=(Spinner)findViewById(R.id.SpNomEntrAssis); 
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this, 
       android.R.layout.simple_spinner_item,ServicePCompanies); 
     spinnerArrayAdapter 
       .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     entrAssistanceList.setAdapter(spinnerArrayAdapter); 

這些行當我這樣做,所以我得到這個錯誤:

E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.NullPointerException 
      at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) 
      at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
      at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:194) 
      at android.widget.Spinner.onMeasure(Spinner.java:454) 
      at android.view.View.measure(View.java:15811) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952) 
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1427) 
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:714) 
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:604) 
      at android.view.View.measure(View.java:15811) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952) 
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1427) 
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:714) 
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:604) 
      at android.view.View.measure(View.java:15811) 
      at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1228) 
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:326) 
      at android.widget.ScrollView.onMeasure(ScrollView.java:321) 
      at android.view.View.measure(View.java:15811) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952) 
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1427) 
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:714) 
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:604) 
      at android.view.View.measure(View.java:15811) 
      at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681) 
      at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) 
      at android.view.View.measure(View.java:15811) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952) 
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:326) 
      at android.view.View.measure(View.java:15811) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952) 
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1427) 
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:714) 
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:604) 
      at android.view.View.measure(View.java:15811) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952) 
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:326) 
      at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2209) 
      at android.view.View.measure(View.java:15811) 
      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2232) 
      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1310) 
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1505) 
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200) 
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4962) 
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776) 
      at android.view.Choreographer.doCallbacks(Choreographer.java:579) 
      at android.view.Choreographer.doFrame(Choreographer.java:548) 
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762) 
      at android.os.Handler.handleCallback(Handler.java:800) 
      at android.os.Handler.dispatchMessage(Handler.java:100) 
      at android.os.Looper.loop(Looper.java:194) 
      at android.app.ActivityThread.main(ActivityThread.java:5449) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:525) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
      at dalvik.system.NativeStart.main(Native Method) 

知道我tryed與我手動創建的陣列以填充微調:

ServicePCompaniesArray=new String[]{"Entreprise1","Entreprise2","Entreprise3"}; 

和我沒有得到錯誤,所以我認爲問題是當我從數據庫中獲取信息,knwoing,我調試它,我給適配器的字符串數組充滿了信息n表示我從數據庫中獲取,所以我真的不知道在哪裏的NullPointerException來自

謝謝你們,

回答

1

你不必須放喲烏爾這個陣列中的數據

String[] ServicePCompanies; 

只需使用你從數據庫中獲取列表

List<ServiceProviderCompanies> mSerProCom=towManager.getTowServiceProviderCompaniesDao().queryForAll(); 

不要忘了覆蓋toString()方法在你的對象(ServiceProviderCompanies)

+0

謝謝,你解決了我的錯誤 – 2015-03-25 09:33:28

0

起初打印您陣列日誌以確保確保有關陣列,然後做到這一點。如果數組包含null,那麼它會給你NullPointerException.So首先檢查數組。你可以放一個if語句來檢查數組是否爲空?

謝謝..

編輯

請更換:

ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this, 
      android.R.layout.simple_spinner_item,ServicePCompanies); 

與此:

ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, ServicePCompanies); 

希望它會運行

+0

您好,感謝爲了您的答案,我已檢查數組是否包含任何null但數組不包含,當我想要顯示微調而不是當我填充它時,崩潰發生,微調器也充滿了我想要顯示的信息,但是當我想要顯示它時,它會崩潰,謝謝 – 2015-03-25 09:04:08

+0

B ut它並沒有解決我的錯誤 – 2015-03-25 09:06:53

+0

請打印整個日誌.. – 2015-03-25 09:07:39