androidandroid-edittextgettextobject-to-string

EditText.getText().toString() shows error


I want user to print his birthday to the edittext field. Then show this information in logs, but my app brake down and I don't know why :) Can you help me?

start_set.xml-----

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/MA_BgColor">
    <TextView
        android:id="@+id/SSA_Tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/SSA_Tv1"
        android:textColor="@color/MA_TvColor"
        android:gravity="center"
        android:textStyle="italic"
        android:typeface="serif"/>

    <EditText
        android:id="@+id/SSA_ETDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="date"
        android:hint="@string/SSA_ETDate"
        android:textColor="@color/SSA_TvColor"
        android:textStyle="italic"
        android:gravity="center_horizontal"/>

    <Button
        android:id="@+id/SSA_BtnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/SSA_BtnAdd"
        android:textColor="@color/MA_TvColor"
        android:gravity="center"
        android:textStyle="italic"
        android:typeface="serif"/>


</LinearLayout>

StartSetActivity.java-----

package ru.rakhmanov.sergey.days;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.widget.*;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.view.View;
import android.widget.Toast;

import java.util.Calendar;

public class StartSetActivity extends Activity implements View.OnClickListener {
Button btnAdd;
TextView tv1;
EditText etDate;
LinearLayout.LayoutParams btnWeight;

String TAG = "Days";
private String DATE;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_set);
    Log.w(TAG, "----- StartSetActivity -----");

    Log.i(TAG, "Start definition");
    Button btnAdd = (Button) findViewById(R.id.SSA_BtnAdd);
    TextView tv1 = (TextView) findViewById(R.id.SSA_Tv1);
    EditText etDate = (EditText) findViewById(R.id.SSA_ETDate);
    LinearLayout.LayoutParams btnWeight = (LinearLayout.LayoutParams) btnAdd.getLayoutParams();

    Log.i(TAG, "Set onClickListeners");
    btnAdd.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    Log.w(TAG, "Start OnClick");
    switch (view.getId()){
        case R.id.SSA_BtnAdd:
            Log.i(TAG, "Visit 'case btnAdd'");
            Log.i(TAG, "Start read inf from ETDate");

            Log.w(TAG, "Date: " + etDate.getText().toString());
            break;
    }
}

It's brake down in Log.w(TAG, "Date: " + etDate.getText().toString());, I think.


Solution

  • Change this

    EditText etDate = (EditText) findViewById(R.id.SSA_ETDate);
    

    to

    etDate = (EditText) findViewById(R.id.SSA_ETDate);
    

    You already have

     EditText etDate; // declared but not initialized
    

    But in onCreate you again declare and then initialize

     EditText etDate = (EditText) findViewById(R.id.SSA_ETDate);
     //etDate is local to onCreate 
    

    Similarly for other variables

    btnAdd = (Button) findViewById(R.id.SSA_BtnAdd);
    tv1 = (TextView) findViewById(R.id.SSA_Tv1);
    etDate = (EditText) findViewById(R.id.SSA_ETDate);