2

所以我正在学习 android 编码,我遇到了 NullPointerException,我不知道为什么。

我知道导致应用程序停止的任何原因都在第 26 行:

LinearLayout billLL = (LinearLayout)findViewById(R.id.billLayout);

怎么错了?xml 文件具有我感兴趣的 id。我非常感谢任何人对此的意见。谢谢

这是TipCalc.java

package com.example.tipcalc;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TipCalc extends Activity {

    private double bill;
    private double tipRate;
    private double tip;

    EditText billET;
    EditText tipRateET;
    EditText tipET;

    TextView billTV;
    TextView tipRateTV;
    TextView tipTV;

    LinearLayout billLL = (LinearLayout)findViewById(R.id.billLayout);
    LinearLayout tipRateLL = (LinearLayout)findViewById(R.id.tipRateLayout);
    LinearLayout tipLL = (LinearLayout)findViewById(R.id.tipLayout);

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

        bill = 0;
        tipRate = .15;
        tip = bill*tipRate;

        billET = (EditText)billLL.findViewById(R.id.editText);
        tipRateET = (EditText)tipRateLL.findViewById(R.id.editText);
        tipET = (EditText)tipLL.findViewById(R.id.editText);

        billTV = (TextView)billLL.findViewById(R.id.name);
        tipRateTV = (TextView)tipRateLL.findViewById(R.id.name);
        tipTV = (TextView)tipLL.findViewById(R.id.name);

        billTV.setText(getString(R.string.bill_total_name));
        tipRateTV.setText(getString(R.string.tip_rate));
        tipTV.setText(getString(R.string.tip_name));

        billET.addTextChangedListener(billListener);
        tipRateET.addTextChangedListener(tipRateListener);

        billET.setText(String.format("%.02f",bill));
        tipRateET.setText(String.format("%.02f",tipRate));
        tipET.setText(String.format("%.02f",tip));
    }

    private TextWatcher billListener = new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {
        }

        @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{
                bill = Double.parseDouble(s.toString());
            }
            catch(NumberFormatException e){
                bill = 0.0;
            }
            updateTip();
        }

    };

    private TextWatcher tipRateListener = new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {
        }

        @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{
                tipRate = Double.parseDouble(s.toString());
            }
            catch(NumberFormatException e){
                tipRate = 0.15;
            }
            updateTip();
        }

    };

    public void updateTip(){
        tip = bill*tipRate;
        tipET.setText(String.format("%.02f", tip));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tip_calc, menu);
        return true;
    }

}

这是activity_tip_calc.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title" />

    <include
        android:id="@+id/billLayout"
        layout="@layout/line" />

    <include
        android:id="@+id/tipRateLayout"
        layout="@layout/line" />

    <include
        android:id="@+id/tipLayout"
        layout="@layout/line" />

</LinearLayout>

这是 line.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="number" >

        <requestFocus />
    </EditText>

</LinearLayout>

这里也是日志

07-31 18:40:30.225: E/AndroidRuntime(31866): FATAL EXCEPTION: main
07-31 18:40:30.225: E/AndroidRuntime(31866): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tipcalc/com.example.tipcalc.TipCalc}: java.lang.NullPointerException
07-31 18:40:30.225: E/AndroidRuntime(31866):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at android.os.Looper.loop(Looper.java:137)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at android.app.ActivityThread.main(ActivityThread.java:5103)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at java.lang.reflect.Method.invokeNative(Native Method)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at java.lang.reflect.Method.invoke(Method.java:525)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at dalvik.system.NativeStart.main(Native Method)
07-31 18:40:30.225: E/AndroidRuntime(31866): Caused by: java.lang.NullPointerException
07-31 18:40:30.225: E/AndroidRuntime(31866):    at android.app.Activity.findViewById(Activity.java:1853)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at com.example.tipcalc.TipCalc.<init>(TipCalc.java:26)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at java.lang.Class.newInstanceImpl(Native Method)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at java.lang.Class.newInstance(Class.java:1130)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-31 18:40:30.225: E/AndroidRuntime(31866):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
07-31 18:40:30.225: E/AndroidRuntime(31866):    ... 11 more
4

4 回答 4

3

你需要运行这一行:

LinearLayout billLL = (LinearLayout)findViewById(R.id.billLayout);

和两条线:

 LinearLayout tipRateLL = (LinearLayout)findViewById(R.id.tipRateLayout);
LinearLayout tipLL = (LinearLayout)findViewById(R.id.tipLayout);

只有在你膨胀后,你才会在这一行中查看:

setContentView(R.layout.activity_tip_calc);

并且activity_tip_calc需要billLayout在其中。虽然活动没有您为其指定的 contentView,但您不能使用实际在其布局中findViewById查找 a 。View

于 2013-08-01T01:55:25.090 回答
2

您在实际设置内容视图之前尝试 findbyId 。id 的任何发现都应该在该行之后

setContentView(R.layout.activity_tip_calc);

所以尝试将所有 3

(LinearLayout)findViewById(R.id.billLayout);

在您实际在 oncreate 中设置内容视图后查看内容。

你得到 Null 的原因是没有布局可以搜索 R.id.billLayout,活动还不知道它的布局是什么,因此无法找到 billLayout。

于 2013-08-01T01:56:08.140 回答
1

只是为了在前面的 2 个中添加一个代码示例,很好地解释了答案(+1 来解释),它应该看起来像这样

LinearLayout billLL, tipRateLL, tipLL;  // declare here so they can be used anywhere in the Activity

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

    // but initialize here
    billLL = (LinearLayout)findViewById(R.id.billLayout);
    tipRateLL = (LinearLayout)findViewById(R.id.tipRateLayout);
    tipLL = (LinearLayout)findViewById(R.id.tipLayout);
于 2013-08-01T02:09:59.583 回答
0

NullPointerException 表示 DVM 无法在该位置找到引用的项目并返回“Null”。

因此,您需要对此进行调查。现在,由于您有日志,它指出您引用 LinearLayout 的 id 的行导致了问题。现在,您必须查看如果您的 R.java 文件中有该 id,仍然是您遇到该问题的原因。因为,您在 activity_tip_calc 布局文件中存在线性布局。因此,您必须在引用布局 ID 之前调用 setContentView(viewID)。谢谢!!

于 2013-08-01T02:06:23.637 回答