EditText.getText()。toString()显示错误

时间:2014-02-17 17:56:22

标签: android android-edittext gettext object-to-string

我希望用户将他的生日打印到edittext字段。然后在日志中显示此信息,但我的应用程序制动失败,我不知道为什么:)你能帮助我吗?

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

我觉得它在Log.w(TAG, "Date: " + etDate.getText().toString());中被制止了。

1 个答案:

答案 0 :(得分:2)

更改此

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

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

你已经拥有

 EditText etDate; // declared but not initialized

但在onCreate中,您再次声明然后初始化

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

与其他变量相似

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