java - 由于包含在布局中的 NullPointerException 而获取 "Unfortunately, my_app has stopped."

标签 java android xml

所以我正在学习 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

最佳答案

你需要运行这一行:

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。虽然 Activity 没有您为其指定的 contentView,但您不能使用 findViewById 在其布局中实际查找 View

关于java - 由于包含在布局中的 NullPointerException 而获取 "Unfortunately, my_app has stopped.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17983964/

相关文章:

java - 反射(reflect) Android Activity 类的注释

java - 在 TLS 重新协商期间更改 TrustManager 的接受发行者

android - 许多 AsyncTask 生成时出现 AsyncTask 性能问题。

java - DialogFragment 中的 getContentResolver() 和 getWindow()

c# - 无法在 C# 中反序列化 XML - InvalidOperationException

java - 每次我单击“登录”按钮时,应用程序都会崩溃

java - 多个GLSurfaceView同时移动

java - 方括号后面跟着大括号难道不会导致数组初始化错误吗?

java - 如果显示键盘,Android 应用程序会崩溃

android - 就像素而言,我可以制作多大的 Android 应用程序 Canvas ?