Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 旋转器,阵列适配器_Android_Spinner_Android Arrayadapter_Android Spinner - Fatal编程技术网

Android 旋转器,阵列适配器

Android 旋转器,阵列适配器,android,spinner,android-arrayadapter,android-spinner,Android,Spinner,Android Arrayadapter,Android Spinner,我对旋转器有问题。我已经设置了ArrayAdapter并设置了MSelected侦听器。但是在onClick方法中,它在els if语句中显示了一个错误 // Create an ArrayAdapter using the string array and a default spinner layout ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,

我对旋转器有问题。我已经设置了ArrayAdapter并设置了MSelected侦听器。但是在onClick方法中,它在els if语句中显示了一个错误

// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.category_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        categorySpinner = (Spinner) findViewById(R.id.editCategorySpinner);

        categorySpinner.setAdapter(adapter);
        categorySpinner.setOnItemSelectedListener(this);

}

private void configureButton2() {
        Button save = (Button) findViewById(R.id.btSave);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (textName.getText().length() == 0) {
                    textName.requestFocus();
                    return;
                } else if (textContent.getText().length() == 0) {
                    textContent.requestFocus();
                    return;
                } else if (categorySpinner.**getText**().length() == 0) {
                    CategorySpinner.requestFocus();
                    return;
                } else {
                    //next Step: get the mood
                    Intent myIntent = new Intent(v.getContext(), Activity2.class);
                myIntent.putExtra("textName", textName.getText().toString());
                myIntent.putExtra("textContent", textContent.**getText**().toString());
                myIntent.putExtra("CategorySpinner", CategorySpinner.**getText()**.toString());
                v.getContext().startActivity(myIntent);
            }
                }
            }
        });
        }

@Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {

        String selected = parent.getItemAtPosition(pos).toString();

    }

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

    }
这不会显示任何错误,但模拟器“不幸停止”


希望您能帮助我。

为什么需要使用
getSelectedItemPosition
方法来获取所选项目的位置。所选项目位置将作为参数传递给
onItemSelected
方法。在您的情况下,变量名称pos将是所选项目的位置

试试这个

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {

    int selectedPosition = pos;

}

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

}
@覆盖
已选择公共位置(AdapterView父项、视图、整数位置、长id){
int selectedPosition=pos;
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}

如果您正在调用
requestFocus(),则您在
else if中看到的错误是什么?else if(categorySpinner.getSelectedItemPosition==0)getSelectedItemPosition无法解决或不是字段使用类名
CategorySpinner
而不是对象
CategorySpinner
?CategorySpinner.getSelectedItemPosition()==0,这是正确的,谢谢,但是我应该在这里做什么:myIntent.putExtra(“CategorySpinner”,CategorySpinner.getText().toString());它显示了以下错误:myIntent.putExtra(“categorySpinner”,categorySpinner.*getText()**.toString());好的,我知道了:)我设置了这个:categorySpinner.getContext().toString()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {

    int selectedPosition = pos;

}

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

}