user3006788
user3006788

Reputation: 165

how to use date and time picker in android and what is the error that i have and how to fix it

I am a creating a simple android application that use date and time picker but the system give me an syntax error how to fix this error ??

The app allow the user to select a date and time than the it will be displayed in a TextView

the error is : in the onclick method in the if statement dialogpick and timepick the error say : timepick/dialogpick cannot be resolved to a variable

MainActivity.java

package com.example.datetime;

import java.text.DateFormat;
import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity implements OnClickListener {
    // btn1=time btn2=date
    Button btn1, btn2;
    TextView txtv;
    Calendar cal = Calendar.getInstance();
    DateFormat dt = DateFormat.getDateTimeInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        txtv = (TextView) findViewById(R.id.textView1);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);

        DatePickerDialog.OnDateSetListener dialogpick = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthofyear,
                    int dayofmonth) {
                // TODO Auto-generated method stub
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, monthofyear);
                cal.set(Calendar.DAY_OF_MONTH, dayofmonth);
                updateText();

            }

            private void updateText() {
                // TODO Auto-generated method stub

            }

        };
        TimePickerDialog.OnTimeSetListener timepick = new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourofday, int minute) {
                // TODO Auto-generated method stub
                cal.set(Calendar.HOUR_OF_DAY, hourofday);
                cal.set(Calendar.MINUTE, minute);
                updateText();

            }

            private void updateText() {
                // TODO Auto-generated method stub

            }

        };

    }

    @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;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if (v.getId() == R.id.button1) {

            new DatePickerDialog(MainActivity.this, dialogpick,
                    cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                    cal.get(Calendar.MONDAY)).show();
        } else if (v.getId() == R.id.button2) {
            new TimePickerDialog(MainActivity.this, timepick,
                    cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE))
                    .show();
        }

    }

}

Upvotes: 0

Views: 2359

Answers (1)

laalto
laalto

Reputation: 152867

TimePickerDialog.OnTimeSetListener timepick = new TimePickerDialog.OnTimeSetListener()

timepick is a local variable in onCreate(). You cannot access local variables in another method such as onClick().

You can move the declaration and instantiation of timepick to class member level, that is outside of onCreate(). You can access member fields from all instance methods.

The same goes for dialogpick.

Upvotes: 1

Related Questions