2017-05-05 45 views
0

我的工作需要在Android Studio中旋轉部件簡單的Android項目的另一項活動。我想要做的是,當用戶從微調器中選擇選項時,它將打開另一個活動。而我正處於編碼的中間。我決定在Intent中使用開關盒條件。問題是,每當我運行應用程序時,它都會自動轉到我聲明的特定活動位置。即使我沒有選擇任何選擇微調。 注:登錄貓不顯示任何錯誤如何解決自動視圖上微調onItemSelected方法

你的幫助是非常感謝給初學者和我一樣。

public class CustomOnItemSelectedListener extends Activity implements 
     OnItemSelectedListener { 

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

     // **************************** below here is where I start the new activity 
     switch (pos) { 
      case 0 : 
       Intent i = new Intent(app.this, home.class); 
       app.this.startActivity(i); 
       break; 

      case 1 : 
       //Intent intent = new Intent(app.this, about.class); 
       //app.this.startActivity(intent); 
       break; 

      case 2 : 
       //Intent intent1 = new Intent(app.this, home.class); 
       //app.this.startActivity(intent1); 
       break; 

     } 
     // **************************** above here is where I start the new activity 
    } 

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

    } 
} 
+0

請重新標記題! – Aroniaina

+0

@Aroniaina抱歉,我的意思是,當我運行應用程序時,它會自動進入home.class。即使我沒有選擇第一個微調。任何想法如何解決我的問題? –

回答

0

在默認情況下,第一個選項是在您的微調選擇創建的應用程序和監聽器被調用。爲了避免這種情況,你可以使用@Simon

的解決方案

,或者你可以用一個按鈕來確認您的選擇,當按下這個按鈕來改變你的活動,而不是當你的微調選擇了一個項目。

UPDATE 您也可以填充你的微調與無用文本中的第一項可以是像「選擇活動」標題,並開始你的聽衆切換到1,而不是0

+0

我已經做了按鈕先生和它的工作..但我想要做的是他們沒有按鈕..我想要的是當用戶選擇任何選擇..默認情況下,它會去其他活動 –

+0

@ J.Doe我更新了我的答案,也許第三個解決方案將幫助你 – Firerazzer

1

試試這個

在你的類,例如聲明一個int在onCreate()之前,然後在您的onCreate()中將其分配給0.使用此變量檢查在使用微調器選擇某個內容時它是否大於0,如下所示。

public class ExampleActivity extends AppCompatActivity { 
private int spinnerCheck; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    mSpinnerCheck = 0; 

    mMySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      int itemId = (int) id; 

      // For some reason this method is called during initialization, so increment counter once to prevent it from auto selecting first item when loading view 
      spinnerCheck += 1; 

      if (spinnerCheck > 1) { 
       switch (pos) { 
        case 0: 
         Intent i = new Intent(app.this, home.class); 
         app.this.startActivity(i); 
         break; 

        case 1: 
         //Intent intent = new Intent(app.this, about.class); 
         //app.this.startActivity(intent); 
         break; 

        case 2: 
         //Intent intent1 = new Intent(app.this, home.class); 
         //app.this.startActivity(intent1); 
         break; 

       } 
      } 
     } 

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

     } 
    }); 
    } 
} 

出於某種原因,它選擇了第一個項目創建後,不知道爲什麼,但這應該工作。

+0

謝謝你的答覆,但它導致了我一個強制關閉。這裏是我的logcat –

+0

產生的原因:顯示java.lang.NullPointerException:試圖調用虛擬方法無效android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView $ OnItemSelectedListener )'在空對象引用 at com.example.joshua.myapplication.app.onCreate(app.java:22) –

+0

第22行是這個先生:spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){ –