import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;public class SpinnerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayAdapter arrayAdapter=ArrayAdapter.createFromResource(this,R.array.planets,R.layout.spinner_item);
        arrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        Spinner classmates=(Spinner)findViewById(R.id.classmates);
        classmates.setAdapter(arrayAdapter);
        
        AdapterView.OnItemSelectedListener l=new AdapterView.OnItemSelectedListener() {
         public void onItemSelected(AdapterView<?>args0,View arg1,int arg2,long arg3){
         Toast.makeText(getApplicationContext(), "This is:"+((TextView)(arg1)).getText()+"==Id:"+arg1.getId()+"arg2="+arg2+"arg3="+arg3,Toast.LENGTH_SHORT).show();
        
         }
         public void onNothingSelected(AdapterView<?>arg0){
}
        };
    classmates.setOnItemSelectedListener(l);
    }
}<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Spinner android:layout_height="wrap_content" android:id="@+id/classmates" android:layout_width="fill_parent"></Spinner>
</LinearLayout><?xml version="1.0" encoding="UTF-8"?>
<CheckedTextView       xmlns:android="http://schemas.android.com/apk/res/android"       android:id="@+id/spinner_dropdown_item"       android:layout_width="wrap_content"       android:layout_height="40dp"    android:singleLine="true"   
    android:textSize="20sp"     style="?android:attr/spinnerDropDownItemStyle"    android:drawableLeft="@drawable/icon"    android:checkMark="@null"/><?xml version="1.0" encoding="UTF-8"?>
<TextView       xmlns:android="http://schemas.android.com/apk/res/android"       android:id="@+id/spinner_item_text"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:singleLine="true"     android:textSize="20sp"/>哪里错了?在线求解。我把全部代码贴出来了。

解决方案 »

  1.   

    public void onItemSelected(AdapterView<?>args0,View arg1,int arg2,long arg3){
       Toast.makeText(getApplicationContext(), "This is:"+((TextView)(arg1)).getText()+"==Id:"+arg1.getId()+"arg2="+arg2+"arg3="+arg3,Toast.LENGTH_SHORT).show();
       
      }
       public void onNothingSelected(AdapterView<?>arg0){
     }
       };
       classmates.setOnItemSelectedListener(l);
       }
    这段错误了,你要把选择监听事件另放一个变量,不要直接写丰大扩号里面,以前我也是像你这样用 出错,改成申明一个变量,然后把变量直接传进去 就没有问题了