我是Android开发新手,我想在我的代码中添加time picker
。我遵循了相同的教程,但是当我使用模拟器时,它不起作用。即使genymotion
告诉我,我的应用程序执行存在问题(我的代码中没有错误),当我将showTimePickerDialog()
放入onCreate()
时,错误就开始了。这是我的代码:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Button button_stpd;
static final int DIALOG_ID = 0 ;
int hour_x;
int minute_x ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showTimePickerDialog();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.Acceuil, new Acceuil()).commit();
showTimePickerDialog();
}
public void showTimePickerDialog(){
button_stpd = (Button)findViewById(R.id.temps_btn);
button_stpd.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID);
}
}
);
}
@Override
protected Dialog onCreateDialog (int id){
if (id == DIALOG_ID)
return new TimePickerDialog(MainActivity.this, kTimePickerListner,hour_x,minute_x,false);
return null;
}
protected TimePickerDialog.OnTimeSetListener kTimePickerListner =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hour_x = hourOfDay;
minute_x = minute;
Toast.makeText(MainActivity.this,hour_x + ":"+minute_x, Toast.LENGTH_SHORT).show();
}
};
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fragmentManager = getFragmentManager() ;
if (id == R.id.nav_Acceuil) {
fragmentManager.beginTransaction().replace(R.id.Acceuil, new Acceuil()).commit() ;
// Handle the camera action
} else if (id == R.id.nav_Messagerie) {
fragmentManager.beginTransaction().replace(R.id.Acceuil, new Messagerie()).commit() ;
} else if (id == R.id.nav_Notification){
fragmentManager.beginTransaction().replace(R.id.Acceuil, new Notification()).commit() ;
} else if (id == R.id.nav_Paramétres) {
fragmentManager.beginTransaction().replace(R.id.Acceuil, new Parametre()).commit() ;
}
else if (id == R.id.nav_Calories) {
fragmentManager.beginTransaction().replace(R.id.Acceuil, new Calcule()).commit() ;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:0)
button_stpd = (Button)findViewById(R.id.temps_btn);
button_stpd.setOnClickListener(...
此代码将您在Java代码中使用的Button与您在xml布局中的Button相关联。这样的代码应该只写一次,因为它是你的一部分 你使用两次的showTimePickerDialog()方法,这是个大问题。
如果你想用另一种方法进行启动,以便onCreate看起来干净,那很好,但只使用该方法一次。例如,它可以被称为initiateViews(),您可以在其中执行以下操作:
button_stpd = (Button)findViewById(R.id.temps_btn);
button_stpd.setOnClickListener(...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
但是,根据我的理解,第二次使用showTimePickerDialog()
时,您真正想要的是showDialog(DIALOG_ID);