无法在Android中获得Spinner的价值

时间:2017-01-08 00:58:40

标签: android listview android-spinner

我有一个微调器,在strings.xml我有一些用于此微调器的项目,我的问题是我无法获得值,错误如下:

我尝试了所有可能的解决方案,我在互联网上找到了没有运气。 我正在使用samsung Note 4和android 6.0.1

E/ViewRootImpl: sendUserActionEvent() mView == null

这是我的代码:

public class activity_mainnew extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

long choose_whale_sound;

@SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_mainnew);

//SPINNER
        Spinner spinner = (Spinner) findViewById(R.id.richard_spinner);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.whale_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
        spinner.setAdapter(adapter);
}


 @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
        choose_whale_sound = id;
        Toast.makeText(activity_mainnew.this, "ID: " + choose_whale_sound, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }


}

1 个答案:

答案 0 :(得分:0)

ArrayAdapter构造函数的定义如下:

ArrayAdapter(Context context, int resource, T[] objects)

有关详情,请点击此链接ArrayAdapter

尝试更改此声明

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.whale_array, android.R.layout.simple_spinner_item);
像这样:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
             android.R.layout.simple_spinner_item,R.array.whale_array);