我的页面上有一个spinner
,使用arrayAdapter
. 当我启动我的应用程序时,一切正常,但是当我单击 时spinner
,应用程序崩溃并且logcat
我没有引用我的代码中的任何一行。这是引用微调器的代码区域:
profiles = (Spinner) findViewById(R.id.spinner1);
SetSql getprofiles = new SetSql(this);
getprofiles.open();
spinnerArray = getprofiles.getProfiles();
if(spinnerArray[0]==null){
spinnerArray = new String[1];
spinnerArray[0] = "No Profiles, Please Create One";
}
getprofiles.close();
ArrayAdapter<String> array = new ArrayAdapter<String>(UserActivity.this, android.R.layout.simple_spinner_item, spinnerArray);
array.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
profiles.setAdapter(array);
//I am using the post method to make sure that this spinner wont run when the activity is started
profiles.post(new Runnable(){
public void run(){
profiles.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> ListView, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
if(!profiles.getItemAtPosition(pos).toString().equals("No Profiles, Please Create One")){
String selected = profiles.getItemAtPosition(pos).toString();
Intent intent = new Intent(UserActivity.this, DPAActivity.class);
intent.putExtra("username", selected);
startActivity(intent);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
});
更多日志:
03-07 16:25:45.740: E/AndroidRuntime(6929): FATAL EXCEPTION: main
03-07 16:25:45.740: E/AndroidRuntime(6929): java.lang.NullPointerException
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:332)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:328)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.AbsListView.obtainView(AbsListView.java:1409)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.ListView.onMeasure(ListView.java:1127)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.view.View.measure(View.java:8313)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.view.View.measure(View.java:8313)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-07 16:25:45.740: E/AndroidRuntime(6929): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.view.View.measure(View.java:8313)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.view.View.measure(View.java:8313)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.view.View.measure(View.java:8313)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.os.Looper.loop(Looper.java:130)
03-07 16:25:45.740: E/AndroidRuntime(6929): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-07 16:25:45.740: E/AndroidRuntime(6929): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 16:25:45.740: E/AndroidRuntime(6929): at java.lang.reflect.Method.invoke(Method.java:507)
03-07 16:25:45.740: E/AndroidRuntime(6929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-07 16:25:45.740: E/AndroidRuntime(6929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-07 16:25:45.740: E/AndroidRuntime(6929): at dalvik.system.NativeStart.main(Native Method)
我的 getProfiles 代码
public void createProfile(String name) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_USER, name);
ourDatabase.insert(TABLE_USERS, null, cv);
}
public String[] getProfiles() throws SQLException{
String[] columns = new String[]{KEY_USER};
Cursor c = ourDatabase.query(TABLE_USERS, columns, null, null, null, null, KEY_ROWID);
String[] result = new String[100];
int iName = c.getColumnIndex(KEY_USER);
int count = 0;
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result[count] = c.getString(iName);
count++;
}
return result;
}
}
这将返回一个字符串数组。