为什么我的吐司不起作用?

Why didn't my toast work?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.item_listview);

    myspinner = (Spinner)findViewById(R.id.spinnertype);
    String provider = myspinner.getSelectedItem().toString();

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, type_arrays,R.layout.support_simple_spinner_dropdown_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    myspinner.setAdapter(adapter);

    myspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            Toast.makeText(parent.getContext(), "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

}
 }

我尝试了不同种类的吐司,但仍然没有出现。什么地方出了错? 我查看了一些帖子,上面说我不能对微调器使用 onclicklistener

你试过了吗,这可能对你有帮助

Toast.makeText(getApplicationContext(), "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();

Toast.makeText(YourActivity.this, "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();

尝试使用 getContext() 而不是 getApplicationContext。

好的,我们再检查一下代码;

正如我所说,您需要使用当前 Activity Context

For example, My Activity name is, MainActivity so, Use: MainActivity.this This should be the context of that Activity.But i think if you use your own code(parent.getContext()) inside the another activity, It won't show up because it's somewhere else actually you know

此外,我猜你在这里做错了(类似的东西:ArrayAdapter.createFromResource issue

我只是引用答案:

Query the data, put it in a List or Array and use this constructor of ArrayAdapter:

ArrayAdapter<CharSequence> dataAdapter1 =  new ArrayAdapter(this, 
                                    android.R.layout.simple_spinner_item, yourArrayOrList);

更新:所以这里是:

Spinner myspinner = (Spinner)findViewById(R.id.spinnertype);
        String provider = myspinner.getSelectedItem().toString();

        ArrayAdapter<CharSequence> adapter =
                new ArrayAdapter<CharSequence>(this, type_arrays, R.layout.support_simple_spinner_dropdown_item);
                /*ArrayAdapter.createFromResource(this, type_arrays,R.layout.support_simple_spinner_dropdown_item);*/

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        myspinner.setAdapter(adapter);

        myspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Toast.makeText(MainActivity.this, "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();

            }

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

            }
        });

现在应该可以了!