将AddTextChangedListener用于MainActivity.Java的onCreate方法时,Android App会崩溃

时间:2014-05-06 03:43:38

标签: android

如果我删除addTextChangedListener,那么该应用程序可以正常运行,但如果我添加addTextChangedListener,那么应用程序会立即崩溃并停止工作。同样在我的布局文件夹中,创建了两个fragement_tip_calc.xmlactivity_tip_calc.xml的文件。我在setContentView()尝试了两种方法,但它对我不起作用。任何帮助,将不胜感激。下面是代码。提前谢谢。

public class TipCalc extends ActionBarActivity {

  // Constants used when saving and restoring

  private static final String TOTAL_BILL = "TOTAL_BILL";
  private static final String CURRENT_TIP = "CURRENT_TIP";
  private static final String BILL_WITHOUT_TIP = "BILL_WITHOUT_TIP";

  private double billBeforeTip; // Users bill before tip
  private double tipAmount; // Tip amount
  private double finalBill; // Bill plus Tip

  EditText billBeforeTipET;
  EditText tipAmountET;
  EditText finalBillET;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tip_calc);

    if (savedInstanceState == null) {
      // Just started
      billBeforeTip = 0.0;
      tipAmount = .15; 
      finalBill = 0.0;
      getSupportFragmentManager().beginTransaction()
      .add(R.id.container, new laceholderFragment()).commit();
    } 
    else 
    {
      billBeforeTip = savedInstanceState.getDouble(BILL_WITHOUT_TIP);
      tipAmount = savedInstanceState.getDouble(CURRENT_TIP); 
      finalBill = savedInstanceState.getDouble(TOTAL_BILL);
    }

    // Initialize the EditTexts

    billBeforeTipET = (EditText) findViewById(R.id.billEditText); // Users bill before tip
    tipAmountET = (EditText) findViewById(R.id.tipEditText); // Tip amount
    finalBillET = (EditText) findViewById(R.id.finalBillEditText); // Bill plus tip

    billBeforeTipET.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
          // TODO Auto-generated method stub
      }

      @Override
      public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {            
          // TODO Auto-generated method stub

      }

      @Override
      public void afterTextChanged(Editable arg0) {
          // TODO Auto-generated method stub
      }
    });
  }
}

fragment_tip_calc.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.webapp.tipcalc.TipCalc$PlaceholderFragment" >

   <TextView
        android:id="@+id/billTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="14dp"
        android:text="@string/bill_text_view" />

    <!-- android:ems defines the width of the EditText box -->

    <EditText
        android:id="@+id/billEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/billTextView"
        android:ems="5"
        android:inputType="numberDecimal"
        android:text="@string/bill_edit_text" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tipTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/billTextView"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/billEditText"
        android:text="@string/tip_text_view" />

    <EditText
        android:id="@+id/tipEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/finalBillTextView"
        android:layout_marginLeft="18dp"
        android:layout_toRightOf="@+id/tipTextView"
        android:ems="4"
        android:inputType="numberDecimal"
        android:text="@string/tip_edit_text" />

    <TextView
        android:id="@+id/finalBillTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/finalBillEditText"
        android:layout_below="@+id/billEditText"
        android:layout_marginTop="14dp"
        android:text="@string/final_bill_text_view" />

    <EditText
        android:id="@+id/finalBillEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/tipEditText"
        android:layout_below="@+id/finalBillTextView"
        android:ems="5"
        android:inputType="numberDecimal"
        android:text="@string/final_bill_edit_text" />


</RelativeLayout>

0 个答案:

没有答案