2016-03-29 80 views
-1

我是新來的android開發,我想添加一個time picker我的代碼。我遵循相同的教程,但是當我使用模擬器時,它不起作用。即使genymotion告訴我執行我的應用時出現問題(並且我的代碼中沒有錯誤),當我將showTimePickerDialog()放入onCreate()時,錯誤開始了。這是我的代碼:時間選擇器android

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

1.什麼是錯誤。 logcat說了些什麼。如果沒有,究竟發生了什麼? 2.請縮進您的代碼:) 3.爲什麼你有2次showTimePickerDialog()? –

+0

偏題:showDialog/onCreateDialog/onPrepareDialog多年來已被棄用。由於Android 3和片段(在本例中爲DialogFragment)出來的確切。 –

回答

0
button_stpd = (Button)findViewById(R.id.temps_btn); 
    button_stpd.setOnClickListener(... 

這段代碼是在Java代碼中你有按鈕鏈接到你在你的XML佈局有按鈕。這樣的代碼應該只寫入一次,因爲它是您使用兩次的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);