作为主题: 根据我当前的工作状态,我可以在设置选项卡和操作栏上显示项目。 但包含的方法不能像预期的那样工作。我不确定是什么问题。
您可以查看以下代码以帮助我理解错误吗?
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;
}
}
}
答案 0 :(得分: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);
}
}