从数据库中填充 Spinner android
Filling a Spinner from DataBase android
我正在做一个项目,我必须从服务器获取一些信息,使用 ormlite 将它们保存在数据库中,然后我想在微调器上显示这些信息,
因此,为了将信息从数据库获取到微调器,我调用了这个方法:
public Dao<ServiceProviderCompanies, Integer> getTowServiceProviderCompaniesDao() {
if (null == towServiceProviderCompaniesDao) {
try {
towServiceProviderCompaniesDao = getDao(ServiceProviderCompanies.class);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
return towServiceProviderCompaniesDao;
}
我在 activity 中这样调用它:
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 来自哪里
谢谢大家,
首先将您的数组打印到 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);
希望它能奏效..
您不必将数据放入此数组中
String[] ServicePCompanies;
只需使用从数据库中获取的列表即可
List<ServiceProviderCompanies> mSerProCom=towManager.getTowServiceProviderCompaniesDao().queryForAll();
不要忘记覆盖对象中的 toString() 方法 (ServiceProviderCompanies)
我正在做一个项目,我必须从服务器获取一些信息,使用 ormlite 将它们保存在数据库中,然后我想在微调器上显示这些信息,
因此,为了将信息从数据库获取到微调器,我调用了这个方法:
public Dao<ServiceProviderCompanies, Integer> getTowServiceProviderCompaniesDao() {
if (null == towServiceProviderCompaniesDao) {
try {
towServiceProviderCompaniesDao = getDao(ServiceProviderCompanies.class);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
return towServiceProviderCompaniesDao;
}
我在 activity 中这样调用它:
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 来自哪里
谢谢大家,
首先将您的数组打印到 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);
希望它能奏效..
您不必将数据放入此数组中
String[] ServicePCompanies;
只需使用从数据库中获取的列表即可
List<ServiceProviderCompanies> mSerProCom=towManager.getTowServiceProviderCompaniesDao().queryForAll();
不要忘记覆盖对象中的 toString() 方法 (ServiceProviderCompanies)