1

我一直在学习 android 应用程序开发,并面临税务应用程序的个人挑战。我在文本编辑器中看不到任何错误,但每当我在 AVD 上运行应用程序时,应用程序甚至在我看到启动页面之前就崩溃了。

我的清单文件

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lorlormensah.taxapp">

    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

我的主要活动

    package com.example.lorlormensah.taxapp;

    import android.app.ActionBar;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;

    public class MainActivity extends AppCompatActivity  implements View.OnClickListener{



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

        }

        EditText salary = (EditText) findViewById(R.id.edit_salary);
        EditText allowance = (EditText) findViewById(R.id.edit_allowance);
        EditText incomeTax = (EditText) findViewById(R.id.IncTax_edit);
        EditText payable = (EditText) findViewById(R.id.payable_edit);
        EditText totalTax = (EditText) findViewById(R.id.deduction_edit);
        Button calculate = (Button) findViewById(R.id.calculate_button);
        final double taxRate = 0.5;
        double tax;


        double realSalary,realAllowance, taxPayable, sniit,firstTax,taxes, levelTax,taxable;


        // @Override
        public void onClick (View v) {

            calculate.setOnClickListener(new Button.OnClickListener() {


                @Override
                public void onClick(View v) {

                    realSalary = Double.parseDouble(salary.getText().toString());
                    realAllowance = Double.parseDouble(allowance.getText().toString());

                    //if(v ==calculate){

                    getSnit();

                    getTaxable();

                    computeTaxes();
                }
                // }

                public double getSnit() {


                    sniit = realSalary * 0.055;


                    return sniit;
                }

                // CALCULATING THE TAXABLE AFTER SNIIT

                public double getTaxable() {
                    if (realSalary <= 216) {

                    } else {
                        taxable = realSalary - sniit;
                    }

                    return taxable;

                }

                public double computeTaxes() {

                    //SOLVING FOR THE SECOND LEVEL(2ND) LEVEL
                    if (taxable > 216 && taxable <= 324) {
                        levelTax = taxable - 216;
                        firstTax = levelTax * 0.05;
                        System.out.println(levelTax);
                        taxes = 0.0 + firstTax;
                        payable.setText("" + taxes);

                        //SOLVING FOR THE THIRD(3RD) LEVEL
                    } else if (taxable > 324 && taxable <= 475) {
                        levelTax = taxable - (216 + 108);
                        firstTax = levelTax * 0.10;
                        System.out.println(levelTax);
                        taxes = 0 + 5.40 + firstTax;
                        payable.setText("" + taxes);

                        //SOLVING FOR THE FOURTH(4TH) LEVEL
                    } else if (taxable > 324 && taxable <= 3240) {
                        levelTax = taxable - (216 + 108 + 151);
                        firstTax = levelTax * 0.175;
                        System.out.println(levelTax);
                        taxes = 0 + 5.40 + 15.10 + firstTax;
                        payable.setText("" + taxes);

                        //SOLVING FOR THE FIFTH(5TH) LEVEL
                    } else if (taxable > 3240) {
                        levelTax = taxable - (216 + 108 + 151 + 2765);
                        firstTax = levelTax * 0.25;
                        //System.out.println("First tax2 " + levelTax);

                        taxes = 0 + 5.40 + 15.10 + 483.88 + firstTax;
                        payable.setText("" + taxes);


                    }
                    // else {
                    //System.out.println("Your salary is tax free");
                    //  }
                    return taxes;
                }


            });
        }
    }

我得到的错误

02-21 17:55:20.314 915-915/com.example.lorlormensah.taxapp D/AndroidRuntime: Shutting down VM
02-21 17:55:20.314 915-915/com.example.lorlormensah.taxapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41465730)
02-21 17:55:20.343 915-915/com.example.lorlormensah.taxapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.lorlormensah.taxapp/com.example.lorlormensah.taxapp.MainActivity}: java.lang.NullPointerException
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
                                                                                 at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                 at android.os.Looper.loop(Looper.java:137)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5103)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                                 at dalvik.system.NativeStart.main(Native Method)
                                                                              Caused by: java.lang.NullPointerException
                                                                                 at android.app.Activity.findViewById(Activity.java:1853)
                                                                                 at com.example.lorlormensah.taxapp.MainActivity.<init>(MainActivity.java:28)
                                                                                 at java.lang.Class.newInstanceImpl(Native Method)
                                                                                 at java.lang.Class.newInstance(Class.java:1130)
                                                                                 at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
                                                                                 at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                 at android.os.Looper.loop(Looper.java:137) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5103) 
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:525) 
                                                                                 at `enter code here`com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                                                                 at dalvik.system.NativeStart.main(Native Method) 
02-21 17:55:22.624 915-915/? I/Process: Sending signal. PID: 915 SIG: 9
4

2 回答 2

1

这些代码行应该在 setContentView() 之后的 onCreate() 中:

    EditText salary = (EditText) findViewById(R.id.edit_salary);
    EditText allowance = (EditText) findViewById(R.id.edit_allowance);
    EditText incomeTax = (EditText) findViewById(R.id.IncTax_edit);
    EditText payable = (EditText) findViewById(R.id.payable_edit);
    EditText totalTax = (EditText) findViewById(R.id.deduction_edit);
    Button calculate = (Button) findViewById(R.id.calculate_button);

目前它们在类定义中,并试图在 setContentView() 使您的视图可用之前执行。在为活动膨胀视图层次结构之前,您不能使用 findViewById。

于 2016-02-22T00:25:18.323 回答
0

正如例外所说:

Caused by: java.lang.NullPointerException
 at android.app.Activity.findViewById(Activity.java:1853)
 at com.example.lorlormensah.taxapp.MainActivity.<init>(MainActivity.java:28)

检查您的第 28 行并在 create 方法中初始化您的 findViewById 字段。

于 2016-02-22T00:14:48.050 回答