2013-08-30 56 views
-3

當我點擊我的應用程序上的按鈕時,我得到日誌上面的錯誤。我將發佈MainActivity和接收活動的代碼。致命例外:主要無法啓動活動componentInfo

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); 

} 

這是我的logcat。

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 
+1

你能後的logcat的輸出 – o0rebelious0o

+0

你甲肝與你的例外堆棧跟蹤,如果是這樣的張貼。 – DaveRlz

+1

可能的重複[FATAL EXCEPTION:主要無法啓動活動componentsInfo。引發java.lang.NullPointerException](http://stackoverflow.com/questions/18534102/fatal-exception-main-unable-to-start-activity-componentsinfo-caused-by-java-la) – thegrinner

回答

0

的問題是要創建一個使用您發送,但你從來不把捆在意向意圖捆綁所以當你設法得到它沒有包獲得

你需要做的calculate.putExtra(extras);

+0

我需要改變我的意圖嗎? –

+0

沒有一切看起來不錯 – tyczj

1

calculate(),其實演員裏面沒有連接到intent.Try

Intent calculate = new Intent(this,Show_returns.class); 
calculate.putExtra(LOW,lowTierInvestString); 
相關問題