Java 时间选择器

Java 时间选择器,java,android,android-studio,Java,Android,Android Studio,我是android开发新手,我想在我的代码中添加一个时间选择器。我遵循了相同的教程,但当我使用模拟器时,它不起作用。即使是genymotion也告诉我,我的应用程序在执行过程中出现了问题(而且代码中没有bug)。当我将showTimePickerDialog()放入onCreate()时,错误就开始了。这是我的代码: public class MainActivity extends AppCompatActivity implements NavigationView.OnNa

我是android开发新手,我想在我的代码中添加一个时间选择器。我遵循了相同的教程,但当我使用模拟器时,它不起作用。即使是
genymotion
也告诉我,我的应用程序在执行过程中出现了问题(而且代码中没有bug)。当我将
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;
    }

}
这段代码将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\u ID)

到底是什么问题?1。错误是什么。logcat说了些什么吗。如果没有,到底发生了什么?2.请缩进您的代码:)3。为什么有两次显示此showTimePickerDialog()?非主题:showDialog/onCreateDialog/onPrepareDialog已被弃用多年。因为Android 3和片段——在本例中是DialogFragment——被证明是精确的。问题是我想在我的项目中添加一个时间选择器,当我喜欢教程时,我不在我的项目中工作,但它在另一个空白活动中工作
 button_stpd = (Button)findViewById(R.id.temps_btn);
 button_stpd.setOnClickListener(... 
 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);