這裏的第一次程序員。所以我使用Eclipse創建了一個權重轉換器,在這裏我輸入一個值並使用2個spinners來選擇單位並在下一個活動中顯示結果。每當它嘗試轉換輸入值時,我的轉換器崩潰。我沒有問題顯示我的輸入值到我的下一個活動,但每當我啓用轉換邏輯它崩潰時,我按轉換按鈕。Android:試圖轉換時貨幣轉換器崩潰
我的主要活動做的轉換和傳遞給其他意圖
package com.example.currencyconverter2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity {
Spinner fromCurrency;
Spinner toCurrency;
public static String final1;
double result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/*protected void onResume{
super.onResume();
setUpSpinnerData();
}*/
//Sets the data for the Spinner to display
public void setSpinnerData(){
fromCurrency = (Spinner)findViewById(R.id.from_currency);
toCurrency = (Spinner)findViewById(R.id.to_currency);
ArrayAdapter <String> from = new ArrayAdapter<String>(this,R.array.currencySpinnerChoices,android.R.layout.simple_list_item_1);
ArrayAdapter <String> to = new ArrayAdapter<String>(this,R.array.currencySpinnerChoices,android.R.layout.simple_list_item_1);
fromCurrency.setAdapter(from);
toCurrency.setAdapter(to);
}
//New intent on button press with inserted data
public void showResult(View v){
Intent i = new Intent(this, MainResultActivity.class);
EditText editconvert = (EditText) findViewById(R.id.amount);
String string1 = editconvert.getText().toString();
//double value = Double.parseDouble(string1);
int cho1 = fromCurrency.getSelectedItemPosition();
int cho2 = toCurrency.getSelectedItemPosition();
double value = Double.parseDouble(editconvert.getText().toString());
double ratio[] = {1.0f, 0.001f, 1.0e-6f, 1.0e-9f};
double result = value/ratio[cho1] * ratio[cho2];
i.putExtra(final1, result);
startActivity(i);
}
}
我的onCreate方法我在其他活動顯示結果
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_result);
//MainActivity.class.getIntent();
Intent intent = getIntent();
double value = intent.getDoubleExtra(MainActivity.final1,0);
TextView tv = (TextView)findViewById(R.id.result_value);
tv.setText(value+" ");
}
我logcat的
12-02 05:50:40.002: E/AndroidRuntime(1528): FATAL EXCEPTION: main
12-02 05:50:40.002: E/AndroidRuntime(1528): Process: com.example.currencyconverter2, PID: 1528
12-02 05:50:40.002: E/AndroidRuntime(1528): java.lang.IllegalStateException: Could not execute method of the activity
12-02 05:50:40.002: E/AndroidRuntime(1528): at android.view.View$1.onClick(View.java:3823)
12-02 05:50:40.002: E/AndroidRuntime(1528): at android.view.View.performClick(View.java:4438)
12-02 05:50:40.002: E/AndroidRuntime(1528): at android.view.View$PerformClick.run(View.java:18422)
12-02 05:50:40.002: E/AndroidRuntime(1528): at android.os.Handler.handleCallback(Handler.java:733)
12-02 05:50:40.002: E/AndroidRuntime(1528): at android.os.Handler.dispatchMessage(Handler.java:95)
12-02 05:50:40.002: E/AndroidRuntime(1528): at android.os.Looper.loop(Looper.java:136)
12-02 05:50:40.002: E/AndroidRuntime(1528): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-02 05:50:40.002: E/AndroidRuntime(1528): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 05:50:40.002: E/AndroidRuntime(1528): at java.lang.reflect.Method.invoke(Method.java:515)
12-02 05:50:40.002: E/AndroidRuntime(1528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-02 05:50:40.002: E/AndroidRuntime(1528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-02 05:50:40.002: E/AndroidRuntime(1528): at dalvik.system.NativeStart.main(Native Method)
12-02 05:50:40.002: E/AndroidRuntime(1528): Caused by: java.lang.reflect.InvocationTargetException
12-02 05:50:40.002: E/AndroidRuntime(1528): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 05:50:40.002: E/AndroidRuntime(1528): at java.lang.reflect.Method.invoke(Method.java:515)
12-02 05:50:40.002: E/AndroidRuntime(1528): at android.view.View$1.onClick(View.java:3818)
12-02 05:50:40.002: E/AndroidRuntime(1528): ... 11 more
12-02 05:50:40.002: E/AndroidRuntime(1528): Caused by: java.lang.NullPointerException
12-02 05:50:40.002: E/AndroidRuntime(1528): at com.example.currencyconverter2.MainActivity.showResult(MainActivity.java:68)
12-02 05:50:40.002: E/AndroidRuntime(1528): ... 14 more
相當新,所以我不明白logcat錯誤,誰能解釋一下?
at com.example.currencyconverter2.MainActivity.showResult(MainActivity.java:68) 檢查此行...這有空指針異常 – ashutiwari4 2014-12-02 11:17:19