在textView上显示文本

时间:2015-09-22 09:16:02

标签: java android timepicker

如何在textview上显示文字?当我点击ok中的TimePicker时,它就崩溃了。

WorkDetails.java

public class WorkDetails extends AppCompatActivity implements View.OnClickListener {

    private static EditText start;
    private static EditText end;
    private static TextView editText;
    private TimePick tp;
    private static int remains;
    private static int startTime,endTime;

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workdetails);

    start = (EditText) findViewById(R.id.editText6);
    end = (EditText) findViewById(R.id.editText7);
    editText=(TextView)findViewById(R.id.textView10);
    tp = new TimePick();
    start.setOnClickListener(this);
    end.setOnClickListener(this);
    addItemsOnSpinner();

}

 public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.editText6) {
            tp.setFlag(TimePick.FLAG_START_DATE);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            tp.show(ft, "TimePicker");
        } else if (id == R.id.editText7) {
            tp.setFlag(TimePick.FLAG_END_DATE);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            tp.show(ft, "TimePicker");
        }

    }

public static class TimePick extends android.app.DialogFragment implements TimePickerDialog.OnTimeSetListener {
            //  EditText time;
            public static final int FLAG_START_DATE = 0;
            public static final int FLAG_END_DATE = 1;
            private int flag = 0;



            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState)
            {
                final Calendar c= Calendar.getInstance();
                int hour=c.get(Calendar.HOUR_OF_DAY);
                int minute=c.get(Calendar.MINUTE);

                return new TimePickerDialog(getActivity(),this,hour,minute, DateFormat.is24HourFormat(getActivity()));
            }

            public void setFlag(int i) {
                flag = i;
            }

            @Override
            public void onTimeSet(TimePicker view,int hourofDay, int minute)
            {

                if (flag == FLAG_START_DATE) {
                    start.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
                    startTime=hourofDay;

                } else if (flag == FLAG_END_DATE) {
                    end.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
                    endTime=hourofDay;
                }
                remains=endTime-startTime;
                 int a=remains;
               editText.setText(a);

               // Toast.makeText(getActivity(),"Time: "+remains,Toast.LENGTH_SHORT).show();

            }

logcat的

Process: com.example.project.project, PID: 3718
    android.content.res.Resources$NotFoundException: String resource ID #0xfffffff7
            at android.content.res.Resources.getText(Resources.java:312)
            at android.widget.TextView.setText(TextView.java:4417)
            at com.example.project.project.WorkDetails$TimePick.onTimeSet(WorkDetails.java:106)
            at android.app.TimePickerDialog.onClick(TimePickerDialog.java:145)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:1)

在你的onTimeSet()'方法,你写

         int a=remains;
           editText.setText(a);

' setText()'方法需要一个String。如果将它与int变量一起使用,则该变量将被解释为唯一的' R.string' xml文件中的字符串资源的标识符' res / values / strings.xml'。

所以你的代码会编译,但在大多数情况下,应用程序会崩溃。

显示' a'在EditText中,将代码更改为

           int a=remains;
           editText.setText("" + a);