を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
}
});
}
});
その他のlogcat:
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;
}
}
これは文字列配列を返しています。