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

                }
            });
        }

    });

更多日志:

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从 Spinner 获取选定项目

于 2013-03-07T13:35:18.110 回答