我希望用户将他的生日打印到编辑文本字段。然后在日志中显示此信息,但我的应用程序崩溃了,我不知道为什么:)你能帮助我吗?
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());
中制动。
最佳答案
改变这个
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);
关于android - EditText.getText().toString() 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21836074/