2014-02-17 59 views
2

我希望用戶將他的生日打印到edittext字段。然後在日誌中顯示這些信息,但我的應用程序剎車不起,我不知道爲什麼:)你能幫我嗎?EditText.getText()。toString()顯示錯誤

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

你declarated 2倍(扣btnAdd; TextView tv1; EditText etDate;) –

回答

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); 
+0

和他應該做的也可能與其他字段相同 – donfuxx

+0

@donfuxx但他沒有使用'onCreate' – Raghunandan

+0

以外的其他變量,還沒有... ;-) – donfuxx

相關問題