我正在开发一个项目,我必须从服务器获取一些信息,使用ormlite将它们保存在数据库中,之后我想在微调器上显示这些信息,
因此,为了从数据库获取信息到spinner,我调用了这个方法:
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();
}
我使用这些代码行填充了我的微调器
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)
知道我试图用我手动创建的数组填充Spinner:
ServicePCompaniesArray=new String[]{"Entreprise1","Entreprise2","Entreprise3"};
并且我没有得到错误所以我认为问题是当我从数据库获取信息时,知道我调试了它并且我给适配器的字符串数组被填充了我从数据库获得的信息,所以我真的不知道NullPointerException来自哪里
谢谢你们,
答案 0 :(得分:1)
您不必将数据放入此数组
String[] ServicePCompanies;
只使用从数据库中获取的列表
List<ServiceProviderCompanies> mSerProCom=towManager.getTowServiceProviderCompaniesDao().queryForAll();
不要忘记在对象(ServiceProviderCompanies)中覆盖toString()方法
答案 1 :(得分:0)
首先将数组打印到Log以确保您的数组然后执行该操作。如果数组包含null,那么它将给你NullPointerException.So首先检查array.Better你可以放一个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);
希望它能起作用..