1

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;
    }
}

これは文字列配列を返しています。

4

2 に答える 2

2
 if(arrayspinner[0]==null){
        arrayspinner = new String[1];
        arrayspinner[0] = "No Profiles, Please Create One";
    }

コードを変更して試してください

編集:

Array使用する代わりにArrayList

List<String> arrayList=Arrays.asList(your string array);

または、応答のサイズを取得して配列を初期化してみてください。

編集2:

List<String> a= Arrays.asList(new String[]{});
        if(a.size()==0){
            a=new ArrayList<String>();
        a.add("xxx");
        a.add("bbbb");
        }
        final Spinner s=(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> array = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, a);
        array.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(array);
        s.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                s.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }
                });
            }
        });

コードと同じサンプルを試してみましたが、動作します。

編集3:

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;
        if(c != null && c.getCount()!=0){
        result = new String[c.getCount()];

        int iName = c.getColumnIndex(KEY_USER);
        int count = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
            result[count] = c.getString(iName);
            count++;
        }
        }else{
        result = new String[]{"No Profiles, Please Create One"};
        }
        return result;
    }
于 2013-03-07T13:41:52.750 に答える
1

内部の if 条件を次のように変更 onItemSelected します。

if(profiles.getItemAtPosition(pos).
                       toString().equals("No Profiles, Please Create One")){
   // your code here....
 }

現在 ListView.getItemAtPosition、スピナーから選択したアイテムを取得するために使用しているため

于 2013-03-07T13:35:18.110 に答える