addTextChangedListener()的空指针异常

时间:2014-05-01 11:36:44

标签: android eclipse

我正在尝试关注Android开发的教程,当我尝试运行代码时,它给了我一个nullpointerexception。任何帮助将不胜感激。这是代码

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crazy_tip_calc);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();

        billBeforeTip = 0.0;
        tipAmount = .15;
        finalBill = 0.0;

    } else {
        billBeforeTip = savedInstanceState.getDouble(BILL_WITHOUT_TIP);
        tipAmount = savedInstanceState.getDouble(CURRENT_TIP);
        finalBill = savedInstanceState.getDouble(TOTAL_BILL);
    }

    billBeforeTipET = (EditText) findViewById(R.id.billEditText);
    tipAmountET = (EditText) findViewById(R.id.tipEditText);
    finalBillET = (EditText) findViewById(R.id.finalBillEditText);

    billBeforeTipET.addTextChangedListener(billBeforeTipListener);
}

private TextWatcher billBeforeTipListener = new TextWatcher() {

    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        try{
            billBeforeTip = Double.parseDouble(s.toString());

        } catch(NumberFormatException e){
            billBeforeTip = 0.0;
        }
        updateTipAndFinalBill();        
    }

};

我已将问题缩小到包含addTextChangedListener()的行,但不知道如何解决问题。

这是日志

05-01 07:00:23.450: E/AndroidRuntime(2052): FATAL EXCEPTION: main
05-01 07:00:23.450: E/AndroidRuntime(2052): Process: ie.ors.crazytipcalc, PID: 2052
05-01 07:00:23.450: E/AndroidRuntime(2052): java.lang.RuntimeException: Unable to start    activity ComponentInfo{ie.ors.crazytipcalc/ie.ors.crazytipcalc.CrazyTipCalc}:  java.lang.NullPointerException
05-01 07:00:23.450: E/AndroidRuntime(2052):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at android.os.Looper.loop(Looper.java:136)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at java.lang.reflect.Method.invokeNative(Native Method)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at java.lang.reflect.Method.invoke(Method.java:515)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at dalvik.system.NativeStart.main(Native Method)
05-01 07:00:23.450: E/AndroidRuntime(2052): Caused by: java.lang.NullPointerException
05-01 07:00:23.450: E/AndroidRuntime(2052):     at ie.ors.crazytipcalc.CrazyTipCalc.onCreate(CrazyTipCalc.java:56)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at android.app.Activity.performCreate(Activity.java:5231)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-01 07:00:23.450: E/AndroidRuntime(2052):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-01 07:00:23.450: E/AndroidRuntime(2052):     ... 11 more
05-01 07:00:27.340: I/Process(2052): Sending signal. PID: 2052 SIG: 9

我的布局代码是     

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

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

<EditText
    android:id="@+id/tipEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/tipTextView"
    android:layout_alignBottom="@+id/tipTextView"
    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/tipEditText"
    android:layout_below="@+id/tipEditText"
    android:layout_marginTop="17dp"
    android:text="@string/final_text_view" />

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

    <requestFocus />
</EditText>

2 个答案:

答案 0 :(得分:0)

我对你的布局文件进行了一些更改..试试这个

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

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

    <EditText
        android:id="@+id/tipEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/tipTextView"
        android:layout_alignBottom="@id/tipTextView"
        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/tipEditText"
        android:layout_below="@id/tipEditText"
        android:layout_marginTop="17dp"
        android:text="@string/final_text_view" />

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

    </EditText>

答案 1 :(得分:0)

崩溃是因为billBeforeTipET为null因为findViewById在Activity onCreate()中找不到它。

在这种情况下,请确保在Activity的setContentView中设置包含带有id billEditText的TextView的布局,而不是Fargment。