这里的第一次程序员。所以我正在使用Eclipse创建一个权重转换器,我输入一个值并使用2个微调器来选择单位并在下一个活动中显示结果。每当尝试转换输入值时,我的转换器就会崩溃。我没有问题将我的输入值显示在我的下一个活动上,但是当我启用转换逻辑时,当我按下转换按钮时它会崩溃。
我的主要活动进行转换并传递给其他意图
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错误,任何人都可以解释一下吗?
答案 0 :(得分:1)
初始化了您的final1
字符串。
public static String final1="result";
以及使用toCurrency
方法
fromCurrency
和setSpinnerData()
答案 1 :(得分:1)
toCurrency
和fromCurrency
从未初始化,因为您在setSpinnerData
中执行了这两个字段的初始化,而这些字段在任何地方都没有被调用。
答案 2 :(得分:0)
> public static String final1;
这从未在您的代码中设置。