我一直在通过税务应用的个人挑战学习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
答案 0 :(得分: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。
答案 1 :(得分: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 method中初始化findViewById字段。