某些代码问题

时间:2018-04-07 18:17:14

标签: android textview runtime-error

我有这个代码的错误,因为当我点击TextView

时应用程序总是崩溃

这是XML代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="dfx.com.asistentepredicaciondfx.MainActivity"
    tools:showIn="@layout/app_bar_main">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toLeftOf="@+id/textView2"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginEnd="18dp"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dia"
        android:onClick="onClick"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toTopOf="@+id/textView3"
        android:layout_marginTop="67dp"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="66dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView3"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Horas"
        android:layout_marginStart="158dp"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView4"
        tools:layout_constraintBaseline_creator="1"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        tools:layout_constraintTop_creator="1"
        android:layout_marginStart="40dp"
        android:layout_marginTop="150dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView3" />

</android.support.constraint.ConstraintLayout>

这是Java代码

   package dfx.com.asistentepredicaciondfx;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * Created by Diego Utreras on 2/3/2018.
 */

public class ContentMain extends Activity implements View.OnClickListener {
    TextView dia;
    int anio;
    int mes;
    int diaM;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        dia = (TextView) findViewById(R.id.textView2);
        dia.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
       final Calendar c = Calendar.getInstance();
        anio = c.get(Calendar.YEAR);
        mes =  c.get(Calendar.MONTH);
        diaM = c.get(Calendar.DAY_OF_YEAR);

        final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, monthOfYear);
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                dia.setText(dateFormat.format(calendar.getTime()));
            }
        }
                ,anio,mes,diaM);
        datePickerDialog.show();

        }
}

在Android工作室中,当我在模拟器中运行它并且单击TextView2以显示应用程序崩溃并关闭的DatePicker时,它不会向我显示任何错误。有人可以告诉我,我需要改变什么,或者我需要为App停止崩溃。

1 个答案:

答案 0 :(得分:0)

这是DatePickerDialog

的默认构造函数
DatePickerDialog (Context context, 
            DatePickerDialog.OnDateSetListener listener, 
            int year, 
            int month, 
            int dayOfMonth)

我发现您的代码存在两个问题。

1)第一个参数是yearanio),您要设置DAY_OF_WEEK(diaM)的值。
2)diaM - 正在为DAY_OF_WEEK设置值,但它应为DAY_OF_MONTH

所以试试这个:

@Override
public void onClick(View view) {
    final Calendar c = Calendar.getInstance();
    anio = c.get(Calendar.YEAR);
    mes =  c.get(Calendar.MONTH);
    diaM = c.get(Calendar.DAY_OF_MONTH);

    final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());

    DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthOfYear);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            dia.setText(dateFormat.format(calendar.getTime()));
        }
    }, anio, mes, diaM);

    datePickerDialog.show();
}