2013-09-25 66 views
0

作爲主題: 隨着我工作的當前狀態,我可以在設置選項卡和操作欄上顯示項目。 但包括的方法不能像預期的那樣工作。我不確定有什麼問題。onOptionsItemSelected項顯示方法不工作?

你可以看看下面的代碼來幫助我理解我的錯誤嗎?

public class Automat extends Activity { 

    DataBase dataBase; 
    ArrayList<Resource> res; 
    ViewPager myPager; 
    boolean move; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_automat); 
     // Show the Up button in the action bar. 
     setupActionBar(); 
     move=true; 


     dataBase = new DataBase(this); 
     dataBase.open(); 

     res = dataBase.getList(); 

     ViewPagerAdapter2 adapter = new ViewPagerAdapter2(this, res, dataBase); 
     System.out.println("ADAPTER" + adapter); 

     myPager = (ViewPager) findViewById(R.id.myPager2); 
     //myPager.setOffscreenPageLimit(0); 
     myPager.setAdapter(adapter); 

     adapter.notifyDataSetChanged(); 
     adapter.startUpdate(myPager); 
     dataBase.close(); 

     class MyTimerTask extends TimerTask { 
      public void run() { 
       runOnUiThread(new Runnable() { 
        public void run() { 
         if (move){    
          myPager.setCurrentItem(myPager.getCurrentItem() + 1); 
         }else {} 

        } 
       }); 

       System.out.println(""); 
      } 
     } 

     MyTimerTask myTask = new MyTimerTask(); 
     Timer myTimer = new Timer(); 

     myTimer.schedule(myTask, 15000, 15000);    
    } 


    /** 
     * Set up the {@link android.app.ActionBar}, if the API is available. 
     */ 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    private void setupActionBar() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.automat, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 

      switchPause(); 

      // This ID represents the Home or Up button. In the case of this 
      // activity, the Up button is shown. Use NavUtils to allow users 
      // to navigate up one level in the application structure. For 
      // more details, see the Navigation pattern on Android Design: 
      // 
      // http://developer.android.com/design/patterns/navigation.html#up-vs-back 
      // 
      NavUtils.navigateUpFromSameTask(this); 

      return true; 
     } 

     return super.onOptionsItemSelected(item);  
    } 

    private void switchPause(){ 
     if(move){ 
      System.out.println("MOVE = FALSE"); 
      move=false; 
     }else { 
      System.out.println("MOVE = true"); 
      move=true; 
     }  
    } 
} 

回答

2

在方法onOptionsItemSelected使用如下代碼以下

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      newGame(); 
      return true; 
     case R.id.help: 
      showHelp(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 
+0

非常感謝你! :) – RobertoB