2014-06-15 76 views
0

我正在創建一個應用程序,其中有一個顯示用戶選項的下拉菜單(微調器),並根據用戶輸入切換活動。我無法理解問題所在。使用微調器在活動之間切換

這裏是我的代碼

public class Main_Calculator extends Activity { 


    Spinner menu; 
    EditText insert; 
    TextView display; 
    Intent f1; 


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



     display=(TextView)findViewById(R.id.display); 
     insert =(EditText)findViewById(R.id.insert); 
     menu = (Spinner)findViewById(R.id.menu); 

     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.menu, android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     menu.setAdapter(adapter); 
     menu.setSelection(0); 

     menu.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       switch (i) 
       { 
        case 2: 
         f1 = new Intent(Main_Calculator.this,Unit_Converter.class); 
         startActivity(f1); 
         break; 

       } 


      } 

      public void onNothingSelected(AdapterView<?> parent) { 
       // Another interface callback 
      } 



     }); 


    } 
} 

這裏是我的錯誤

06-16 00:11:45.309 10726-10726/com.sams.ultimateallpurpose.calculator E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.sams.ultimateallpurpose.calculator, PID: 10726 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sams.ultimateallpurpose.calculator/com.sams.ultimateallpurpose.calculator.Main_Calculator}: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner. 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
      at android.app.ActivityThread.access$800(ActivityThread.java:135) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5001) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner. 
      at android.widget.Spinner.setOnItemClickListener(Spinner.java:461) 
      at com.sams.ultimateallpurpose.calculator.Main_Calculator.onCreate(Main_Calculator.java:64) 
      at android.app.Activity.performCreate(Activity.java:5231) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
            at android.app.ActivityThread.access$800(ActivityThread.java:135) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5001) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
            at dalvik.system.NativeStart.main(Native Method) 

回答

4

唯一的例外是簡單的:你不能使用單擊帶有微調監聽器。的Spinner.java源代碼:

/** 
* <p>A spinner does not support item click events. Calling this method 
* will raise an exception.</p> 
* <p>Instead use {@link AdapterView#setOnItemSelectedListener}. 
* 
* @param l this listener will be ignored 
*/ 
@Override 
public void setOnItemClickListener(OnItemClickListener l) { 
    throw new RuntimeException("setOnItemClickListener cannot be used with a spinner."); 
} 

是的,醫生建議使用setOnItemSelectedListener方法來代替。

+0

謝謝..我得到了我的問題..我沒有改變setOnItemSelectedListener –