当我尝试转到App中的Next Activity Page时,应用程序崩溃了

时间:2016-08-27 00:31:12

标签: java android error-handling logcat

我正在制作一个非常基本的6-7活动应用程序,试图让它变得非常简单。我已成功添加了4个其他实例,在应用程序的底部按“NEXT”,它将带您进入应用程序中的下一个活动。但是,我陷入困境,无法弄清楚如何继续。我试过调试,无法弄清楚错误信息的含义。谢谢你的帮助。

来自Logcat的错误消息:

java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                 at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                                 at android.view.View.performClick(View.java:4785)
                                                                                 at android.view.View$PerformClick.run(View.java:19884)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                                              Caused by: java.lang.reflect.InvocationTargetException
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                                 at android.view.View.performClick(View.java:4785) 
                                                                                 at android.view.View$PerformClick.run(View.java:19884) 
                                                                                 at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                 at android.os.Looper.loop(Looper.java:135) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
                                                                                 at com.example.android.budgeit10.expenses.fourthNext(expenses.java:86)
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                 at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                                 at android.view.View.performClick(View.java:4785) 
                                                                                 at android.view.View$PerformClick.run(View.java:19884) 
                                                                                 at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                 at android.os.Looper.loop(Looper.java:135) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

 

JAVA:

        package com.example.android.budgeit10;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class expenses extends AppCompatActivity {

        AppCompatActivity appCompatActivity;
        EditText housingExpense;
        EditText foodExpense;
        EditText transportExpense;
        EditText otherExpense;
        Button expensesButton;
        TextView addExpenses;
        EditText income;

        double housing, food, transport, other, sum;


        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_expenses);
            housingExpense = (EditText) findViewById(R.id.housingExpense);
            foodExpense = (EditText) findViewById(R.id.foodExpense);
            transportExpense = (EditText) findViewById(R.id.transportExpense);
            otherExpense = (EditText) findViewById(R.id.otherExpense);
            expensesButton = (Button) findViewById(R.id.expensesButton);
            addExpenses = (TextView) findViewById(R.id.totalMonthlyExpenses);
            income = (EditText) findViewById(R.id.monthlyIncome);


            expensesButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    housing = Double.parseDouble(housingExpense.getText().toString());
                    try {
                        Double.parseDouble(housingExpense.getText().toString());
                    } catch (NumberFormatException nfe) {
                    }
                    food = Double.parseDouble(foodExpense.getText().toString());
                    transport = Double.parseDouble(transportExpense.getText().toString());
                    other = Double.parseDouble(otherExpense.getText().toString());
                    sum = housing + food + transport + other;
                    addExpenses.setText(Double.toString(sum));

                }
            });

            expensesButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                    if (TextUtils.isEmpty(housingExpense.getText().toString()) ||
                            TextUtils.isEmpty(foodExpense.getText().toString()) ||
                            TextUtils.isEmpty(otherExpense.getText().toString()) ||
                            TextUtils.isEmpty(transportExpense.getText().toString())) {

                        addExpenses.setText("Please fill all fields."); // if any of the fields is empty, add nothing to textview

                    } else {
                        Double housing = Double.parseDouble(housingExpense.getText().toString());
                        Double food = Double.parseDouble(foodExpense.getText().toString());
                        Double transport = Double.parseDouble(transportExpense.getText().toString());
                        Double other = Double.parseDouble(otherExpense.getText().toString());
                        Double sum = housing + food + transport + other;
                        addExpenses.setText(Double.toString(sum));
                    }
                }
            });
        }
        public void fourthNext(View view) {
            Intent intent = new Intent(this, Goals.class);
            TextView textView = (TextView) findViewById(R.id.totalMonthlyIncome);

            String income = textView.getText().toString();
            startActivity(intent);

        }
来自Button的

XML:

<LinearLayout 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:orientation="vertical"
    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.example.android.budgeit10.incomePage">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="MONTHLY EXPENSES"
        android:textSize="24dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/totalMonthlyIncome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="16dp"
            android:paddingTop="16dp"
            android:text="Enter Monthly Income"
            android:textSize="22dp"
            android:textStyle="bold" />


        <EditText
            android:id="@+id/monthlyIncome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/income_on_expenses"
            android:inputType="phone"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:textSize="16dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#2E7D32" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="HOUSING"
            android:textSize="22dp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/housingExpense"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/housing"
            android:inputType="phone"
            android:paddingLeft="16dp"
            android:paddingRight="16dp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:paddingTop="16dp"
            android:text="FOOD"
            android:textSize="22dp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/foodExpense"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/food"
            android:inputType="phone"
            android:paddingLeft="16dp"
            android:paddingRight="16dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:paddingTop="16dp"
            android:text="TRANSPORTATION"
            android:textSize="22dp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/transportExpense"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/transport"
            android:inputType="phone"
            android:paddingLeft="16dp"
            android:paddingRight="16dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:paddingTop="16dp"
            android:text="EVERYTHING ELSE"
            android:textSize="22dp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/otherExpense"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/other"
            android:inputType="phone"
            android:paddingLeft="16dp"
            android:paddingRight="16dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#2E7D32" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="16dp">

        <Button
            android:id="@+id/expensesButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#C8E6C9"
            android:onClick="submitOrder"
            android:padding="16dp"
            android:text="TOTAL EXPENSES"
            android:textSize="16dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/totalMonthlyExpenses"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/monthly_expenses"
            android:inputType="phone"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:textSize="16dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:text="Extra $ : "
            android:textSize="24dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/extra_cash"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/extra_cash"
            android:inputType="phone"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:textSize="16dp" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/fourthNext"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_alignParentBottom="true"
            android:background="#C8E6C9"
            android:onClick="fourthNext"
            android:text="NEXT"
            android:textSize="34dp">

        </Button>
    </RelativeLayout>


</LinearLayout>

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.budgeit10">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".PageTwo" />
    <activity android:name=".incomePage" />
    <activity android:name=".expenses" />
    <activity android:name=".Goals" />
</application>

1 个答案:

答案 0 :(得分:0)

你应该有一个带有id&#39; totalMonthlyIncome&#39;的TextView。在您的XML布局中。 (根据Java代码)只需确保它在那里,它将解决问题。

&#34;引起:java.lang.NullPointerException:尝试调用虚方法&#39; java.lang.CharSequence android.widget.TextView.getText()&#39;在空对象引用上

at com.example.android.budgeit10.expenses.fourthNext( expenses.java:86 )&#34;

根据logcat错误在expense.java中 - 第86行