Android Studio App会立即崩溃 - java.lang.NullPointerException

时间:2017-10-12 03:05:36

标签: java android nullpointerexception

继续尝试运行我的应用但收到以下错误:

" java.lang.NullPointerException:尝试调用虚方法' android.text.Editable android.widget.EditText.getText()'在null对象引用"

只要我真的尝试使用该页面上的变量运行任何代码,就会发生这种情况。简单地声明他们的ID不会造成任何错误,我想因为他们从未被调用过。

我不确定我的错误。我知道我需要检查解析,因为可能会发生NumberException,但它甚至无法达到这一点,我也不知道为什么。即使是基本的.getText()也会导致崩溃。

我检查了重复问题的链接,但我无法确定我未正确初始化的位置。

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

    // variable declarations for reservation details
    final EditText emailInput = (EditText) findViewById(R.id.emailInput);
    final EditText phoneInput = (EditText) findViewById(R.id.phoneInput);
    final EditText dateInput = (EditText) findViewById(R.id.dateInput);
    final EditText totalAdults = (EditText) findViewById(R.id.totalAdults);
    final EditText totalKids = (EditText) findViewById(R.id.totalKids);

    // declare next and back buttons for all pages
    final Button nextToDinner = (Button) findViewById(R.id.nextToDinner);
    final Button backToReservation = (Button) findViewById(R.id.backToReservation);
    final Button nextToProcessing = (Button) findViewById(R.id.nextToProcessing);

    // spinner declarations
    final Spinner countySpinner = (Spinner) findViewById(R.id.countySpinner);
    final Spinner timeSpinner = (Spinner) findViewById(R.id.timeSpinner);

    // variable declarations for dinner choices
    final EditText tendiesInput = (EditText) findViewById(R.id.tendiesInput);
    final EditText crabInput = (EditText) findViewById(R.id.crabInput);
    final EditText saladInput = (EditText) findViewById(R.id.saladInput);
    final EditText wingsInput = (EditText) findViewById(R.id.wingsInput);
    final EditText soupInput = (EditText) findViewById(R.id.soupInput);
    final EditText fishDinnerInput = (EditText) findViewById(R.id.fishDinnerInput);
    final EditText steakDinnerInput = (EditText) findViewById(R.id.steakDinnerInput);
    final EditText chickenDinnerInput = (EditText) findViewById(R.id.chickenDinnerInput);
    final EditText kidsDinnerInput = (EditText) findViewById(R.id.kidsDinnerInput);
    final TextView totalAmount = (TextView) findViewById(R.id.totalAmount);

    // declare starter prices
    final double chickenTendies = 10.99;
    final double crabDip = 13.99;
    final double salad = 5.99;
    final double buffaloWings = 8.99;
    final double soup = 6.99;

    // declare dinner prices
    final double fishDinner = 26.99;
    final double steakDinner = 31.99;
    final double chickenDinner = 24.99;
    final double kidsDinner = 15.99;

    // variable declarations for confirmation page
    final TextView confirmEmail = (TextView) findViewById(R.id.confirmEmail);
    final TextView confirmPhone = (TextView) findViewById(R.id.confirmPhone);
    final TextView confirmCounty = (TextView) findViewById(R.id.confirmCounty);
    final TextView confirmTime = (TextView) findViewById(R.id.confirmTime);
    final TextView confirmDate = (TextView) findViewById(R.id.confirmDate);
    final TextView confirmAdults = (TextView) findViewById(R.id.confirmAdults);
    final TextView confirmKids = (TextView) findViewById(R.id.confirmKids);
    final TextView confirmApps = (TextView) findViewById(R.id.confirmApps);
    final TextView confirmDinners = (TextView) findViewById(R.id.confirmDinners);
    final TextView confirmTotal = (TextView) findViewById(R.id.confirmTotal);
    final TextView confirmAddress = (TextView) findViewById(R.id.confirmAddress);

    // get user input for reservation details and store in variable(s)
    final String storeEmail = emailInput.getText().toString();
    final String storePhone = phoneInput.getText().toString();
    final String storeDate = dateInput.getText().toString();
    final String storeAdults = totalAdults.getText().toString();
    final String storeKids = totalKids.getText().toString();

    // declare array adapters for county and time
    ArrayAdapter countiesArray = new ArrayAdapter(this, android.R.layout.simple_selectable_list_item, county);
    ArrayAdapter timeArray = new ArrayAdapter(this, android.R.layout.simple_selectable_list_item, time);

    countySpinner.setOnItemSelectedListener(this);
    timeSpinner.setOnItemSelectedListener(this);

    countySpinner.setAdapter(countiesArray);
    timeSpinner.setAdapter(timeArray);

** these lines break the code even before a NumberException can occur**
    // get user input for appetizers and parse to double variables for calculation
    final double tendiesNum = Double.parseDouble(tendiesInput.getText().toString());
    final double crabNum = Double.parseDouble(crabInput.getText().toString());
    final double saladNum = Double.parseDouble(saladInput.getText().toString());
    final double wingsNum = Double.parseDouble(wingsInput.getText().toString());
    final double soupNum = Double.parseDouble(soupInput.getText().toString());

    // get user input for dinners and parse to double variables for calculation
    final double fishNum = Double.parseDouble(fishDinnerInput.getText().toString());
    final double chickenNum = Double.parseDouble(chickenDinnerInput.getText().toString());
    final double steakNum = Double.parseDouble(steakDinnerInput.getText().toString());
    final double kidsNum = Double.parseDouble(kidsDinnerInput.getText().toString());

各页的XML:

<Button
    android:id="@+id/backToReservation"
    android:layout_width="75dp"
    android:layout_height="40dp"
    android:text="BACK"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="293dp"
    app:layout_constraintRight_toRightOf="parent" />

<Button
    android:id="@+id/nextToProcessing"
    android:layout_width="85dp"
    android:layout_height="40dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="259dp"
    android:text="CONFIRM"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/starterPrice1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$10.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/tendiesText" />

<TextView
    android:id="@+id/starterPrice2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$13.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/crabText" />

<TextView
    android:id="@+id/starterPrice3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$5.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="124dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/saladText" />

<TextView
    android:id="@+id/starterPrice4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$8.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="124dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/wingsText" />

<TextView
    android:id="@+id/starterPrice5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$6.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="124dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/soupText" />

<TextView
    android:id="@+id/dinnerPrice1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$26.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/fishDinnerText" />

<TextView
    android:id="@+id/dinnerPrice2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$31.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/steakDinnerText" />

<TextView
    android:id="@+id/dinnerPrice3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$24.99"
    android:textSize="10sp"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/chickenDinnerText" />

<TextView
    android:id="@+id/dinnerPrice4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$15.99"
    android:textSize="10sp"
    android:textStyle="italic"
    android:layout_marginLeft="119dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/kidsDinnerText" />

<TextView
    android:id="@+id/totalAmount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="178dp"
    android:text="Total: "
    app:layout_constraintTop_toBottomOf="@+id/dinnerTitle"
    android:layout_marginLeft="109dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/menuText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Menu"
    android:textAlignment="center"
    android:textSize="24sp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="162dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/appetizerTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Appetizers"
    android:textSize="18sp"
    android:textStyle="bold"
    android:layout_marginTop="2dp"
    app:layout_constraintTop_toBottomOf="@+id/menuText"
    android:layout_marginLeft="149dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/tendiesText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Chicken Tenders:"
    android:textAlignment="textStart"
    android:textSize="16sp"
    android:layout_marginLeft="29dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/tendiesInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/tendiesInput"
    android:layout_width="100dp"
    android:layout_height="35dp"
    android:layout_marginLeft="100dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:inputType="number"
    android:textAlignment="center"
    android:textSize="12sp"
    app:layout_constraintLeft_toRightOf="@+id/tendiesText"
    app:layout_constraintTop_toBottomOf="@+id/appetizerTitle" />

<TextView
    android:id="@+id/crabText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Crab Dip:"
    android:textSize="16sp"
    android:layout_marginLeft="85dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/crabInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/crabInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/tendiesInput"
    app:layout_constraintLeft_toRightOf="@+id/crabText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/wingsText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Buffalo Wings:"
    android:textSize="16sp"
    android:layout_marginLeft="47dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/wingsInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/wingsInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/saladInput"
    app:layout_constraintLeft_toRightOf="@+id/wingsText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/soupText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Soup Special:"
    android:textSize="16sp"
    android:layout_marginLeft="57dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/soupInput"
    android:layout_marginTop="-4dp" />

<EditText
    android:id="@+id/soupInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/wingsInput"
    app:layout_constraintLeft_toRightOf="@+id/soupText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/saladText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Salad:"
    android:textSize="16sp"
    android:layout_marginLeft="107dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/saladInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/saladInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/crabInput"
    app:layout_constraintLeft_toRightOf="@+id/saladText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/dinnerTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dinners"
    android:textSize="18sp"
    android:textStyle="bold"
    android:layout_marginTop="205dp"
    app:layout_constraintTop_toBottomOf="@+id/appetizerTitle"
    android:layout_marginLeft="161dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/fishDinnerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fish:"
    android:textSize="16sp"
    android:layout_marginLeft="116dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/fishDinnerInput"
    android:layout_marginTop="-8dp" />

<EditText
    android:id="@+id/fishDinnerInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/dinnerTitle"
    app:layout_constraintLeft_toRightOf="@+id/fishDinnerText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/steakDinnerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Steak:"
    android:textSize="16sp"
    android:layout_marginLeft="106dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/steakDinnerInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/steakDinnerInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/fishDinnerInput"
    app:layout_constraintLeft_toRightOf="@+id/steakDinnerText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/chickenDinnerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Chicken:"
    android:textSize="16sp"
    android:layout_marginLeft="90dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/chickenDinnerInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/chickenDinnerInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/steakDinnerInput"
    app:layout_constraintLeft_toRightOf="@+id/chickenDinnerText"
    android:layout_marginLeft="100dp" />

<TextView
    android:id="@+id/kidsDinnerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Kid's Choice"
    android:textSize="16sp"
    android:layout_marginLeft="65dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/kidsDinnerInput"
    android:layout_marginTop="0dp" />

<EditText
    android:id="@+id/kidsDinnerInput"
    android:layout_width="100dp"
    android:layout_height="25dp"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/chickenDinnerInput"
    app:layout_constraintLeft_toRightOf="@+id/kidsDinnerText"
    android:layout_marginLeft="100dp" />

错误日志:

                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.dtcc.dinnerreservationapp/edu.dtcc.dinnerreservationapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:148)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                 at edu.dtcc.dinnerreservationapp.MainActivity.onCreate(MainActivity.java:116)
                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:148) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

您要从中获取文字的编辑文字 id未在代码中正确添加 重新检查你的id分配编辑文本