当我单击应用程序上的按钮时,我在日志中收到上述错误。我将发布 MainActivity 和接收活动的代码。
protected void onCreate(Bundle savedInstanceState)throws NullPointerException {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.calculate);
button.setOnClickListener(onSave);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
View.OnClickListener onSave = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.getId() == R.id.calculate)
calculate(v);
}
};
在 onClick 上计算上面是我的计算方法,我将在下面发布。
public void calculate(View view){
Intent calculate = new Intent(this,Show_returns.class);
Bundle extras = new Bundle();
EditText monthlySalaryString = (EditText)findViewById(R.id.monthly_salary);
double monthlySalary = stripRFromInputString(monthlySalaryString.getText().toString());
EditText ageString = (EditText)findViewById(R.id.age);
int age = Integer.parseInt(ageString.getText().toString());
double tax = ComputeTax.taxableCategory(age, monthlySalary);
double annualSalary = monthlySalary * 12;
EditText percInvestView = (EditText) findViewById(R.id.perc_invest);
double percInvest = stripRFromInputString(percInvestView.getText().toString())/100;
EditText numYearsInvestString = (EditText) findViewById(R.id.num_years_invest);
int numYearsInvest = Integer.parseInt(numYearsInvestString.getText().toString());
EditText lowTierInvestView = (EditText) findViewById(R.id.low_tier_invest);
double lowTierInvest = stripRFromInputString(lowTierInvestView.getText().toString())/100;
EditText medTierInvestView = (EditText) findViewById(R.id.med_tier_invest);
double medTierInvest = stripRFromInputString(medTierInvestView.getText().toString())/100;
EditText highTierInvestView = (EditText)findViewById(R.id.high_tier_invest);
double highTierInvest = stripRFromInputString(highTierInvestView.getText().toString())/100;
double lowTierInvestCompound = Investments.lowTierInvestmentReturns(annualSalary, tax, numYearsInvest, percInvest, lowTierInvest);
double medTierInvestCompound = Investments.lowTierInvestmentReturns(annualSalary, tax, numYearsInvest, percInvest,medTierInvest);
double highTierInvestCompound = Investments.lowTierInvestmentReturns(annualSalary, tax, numYearsInvest, percInvest,highTierInvest);
String lowTierInvestString = String.valueOf(Math.floor(lowTierInvestCompound));
String medTierInvestString = String.valueOf(Math.floor(medTierInvestCompound));
String highTierInvestString = String.valueOf(Math.floor(highTierInvestCompound));
extras.putString(LOW,lowTierInvestString);
extras.putString(MED,medTierInvestString);
extras.putString(HIGH,highTierInvestString);
extras.putDouble(TAX,tax);
startActivity(calculate);
}
下面是接收活动的代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent receive =getIntent();
Bundle extras = receive.getExtras();
String lowTierInvest = extras.getString(MainActivity.LOW);
String medTierInvest = extras.getString(MainActivity.MED);
String highTierInvest =extras.getString(MainActivity.HIGH);
double tax = extras.getDouble(MainActivity.TAX);
String TaxDisplay = String.format("Total annual tax amount is: R%0.2fc", tax);
String lowTierDisplay = String.format("Compounded amount for low tier after 3 years is: R%0.2c", lowTierInvest);
String medTierDisplay = String.format("Compounded amount for medium tier after 3 years is: R%0.2fc", medTierInvest);
String highTierDisplay = String.format("Compounded amount for medium tier after 3 years is: R%0.2fc", highTierInvest);
TextView textView = new TextView(this);
textView.setTextSize(18);
textView.setText(TaxDisplay +" \n"+ lowTierDisplay +"\n" +medTierDisplay + "\n" +highTierDisplay);
setContentView(textView);
}
这是我的日志猫。
08-30 09:03:07.667: E/AndroidRuntime(1237): FATAL EXCEPTION: main
08-30 09:03:07.667: E/AndroidRuntime(1237): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.investments/com.example.investments.Show_returns}: java.lang.NullPointerException
08-30 09:03:07.667: E/AndroidRuntime(1237): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-30 09:03:07.667: E/AndroidRuntime(1237): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-30 09:03:07.667: E/AndroidRuntime(1237): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-30 09:03:07.667: E/AndroidRuntime(1237): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-30 09:03:07.667: E/AndroidRuntime(1237): at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 09:03:07.667: E/AndroidRuntime(1237): at android.os.Looper.loop(Looper.java:137)
08-30 09:03:07.667: E/AndroidRuntime(1237): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-30 09:03:07.667: E/AndroidRuntime(1237): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 09:03:07.667: E/AndroidRuntime(1237): at java.lang.reflect.Method.invoke(Method.java:525)
08-30 09:03:07.667: E/AndroidRuntime(1237): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-30 09:03:07.667: E/AndroidRuntime(1237): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-30 09:03:07.667: E/AndroidRuntime(1237): at dalvik.system.NativeStart.main(Native Method)
08-30 09:03:07.667: E/AndroidRuntime(1237): Caused by: java.lang.NullPointerException
08-30 09:03:07.667: E/AndroidRuntime(1237): at com.example.investments.Show_returns.onCreate(Show_returns.java:20)
08-30 09:03:07.667: E/AndroidRuntime(1237): at android.app.Activity.performCreate(Activity.java:5133)
08-30 09:03:07.667: E/AndroidRuntime(1237): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-30 09:03:07.667: E/AndroidRuntime(1237): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-30 09:03:07.667: E/AndroidRuntime(1237): ... 11 more