Java 无法访问数组的值
我正在尝试编写一个货币转换器,在那里我想从yahoo finance获取信息,然后将其保存到数据库中。到目前为止,一切正常,除了一件事,我无法访问速率[k]的值,我唯一可以访问速率[k]的地方是按钮内部,我不想,我想获取它的值,并将其保存到数据库中,然后在按下按钮之前检索它,你能帮我吗Java 无法访问数组的值,java,android,Java,Android,我正在尝试编写一个货币转换器,在那里我想从yahoo finance获取信息,然后将其保存到数据库中。到目前为止,一切正常,除了一件事,我无法访问速率[k]的值,我唯一可以访问速率[k]的地方是按钮内部,我不想,我想获取它的值,并将其保存到数据库中,然后在按下按钮之前检索它,你能帮我吗 public class currency extends Activity{ int item1, item2; private String array_spinner[];
public class currency extends Activity{
int item1,
item2;
private String array_spinner[];
private String array_spinner2[];
public Double rates[],
obj4;
public String API_URL2,
rates_new[],
s_query;
public SQLiteDatabase db;
@ Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currency1);
array_spinner = new String[38];
rates = new Double[38];
rates_new = new String[38];
array_spinner[0] = " USD US Dollar";
rates[0] = 1.0;
db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
db.execSQL("DROP TABLE IF EXISTS FunnyNames");
db.execSQL("CREATE TABLE IF NOT EXISTS FunnyNames (id INTEGER, Email DOUBLE, FirstName VARCHAR, LastName VARCHAR);");
db.execSQL("INSERT INTO FunnyNames VALUES('0','" + rates[0] + "', 'Anita', 'Bath');");
for (int i = 1; i <= 37; i++) {
final int j = i;
API_URL2 = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USD"
+ array_spinner2[i]
+ "%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
AsyncHttpClient client = new AsyncHttpClient();
client.get(API_URL2, new AsyncHttpResponseHandler() {
@ Override
public void onFailure(Throwable arg0, String arg1) {
super.onFailure(arg0, arg1);
}
@ Override
public void onFinish() {
super.onFinish();
}
@ Override
public void onStart() {
super.onStart();
}
@ Override
public void onSuccess(String response) {
try {
JSONObject jsonObj = new JSONObject(response);
JSONObject obj1 = jsonObj.getJSONObject("query");
JSONObject obj2 = obj1.getJSONObject("results");
JSONObject obj3 = obj2.getJSONObject("rate");
obj4 = obj3.getDouble("Rate");
rates[j] = obj4;
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
for (int k = 0; k <= 37; k++) {
s_query = "INSERT INTO FunnyNames VALUES('" + k + "','" + rates[k] + "', 'Anita', 'Bath');";
db.execSQL(s_query);
String select_query = "SELECT * from FunnyNames WHERE id=" + k;
Cursor c = db.rawQuery(select_query, null);
c.moveToFirst();
rates_new[k] = c.getString(1);
}
final Spinner s1 = (Spinner)findViewById(R.id.spinner23);
ArrayAdapter adapter1 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s1.setAdapter(adapter1);
adapter1.setDropDownViewResource(R.layout.spinner_layout);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView < ? > argo0, View arg1,
int arg2, long arg3) {
int item = s1.getSelectedItemPosition();
item1 = item;
}
public void onNothingSelected(AdapterView < ? > arg0) {}
});
final Spinner s2 = (Spinner)findViewById(R.id.spinner24);
ArrayAdapter adapter2 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s2.setAdapter(adapter2);
adapter2.setDropDownViewResource(R.layout.spinner_layout);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView < ? > argo0, View arg1,
int arg2, long arg3) {
int item = s2.getSelectedItemPosition();
item2 = item;
}
public void onNothingSelected(AdapterView < ? > arg0) {}
});
Button currnecy_convert = (Button)findViewById(R.id.Button12);
currnecy_convert.setOnClickListener(new OnClickListener() {
@ Override
public void onClick(View arg0) {
final EditText input_currency = (EditText)findViewById(R.id.editText11);
final TextView converted_currency = (TextView)findViewById(R.id.textView61);
if (!input_currency.getText().toString().equals("")) {
converted_currency.setText(String.format(" " + rates_new[6] + " " + rates_new[20] + " " + rates_new[31]));
} else {
Toast.makeText(getApplicationContext(), "Please enter a USD value!", Toast.LENGTH_LONG).show();
}
}
});
}
}
公共类货币扩展活动{
int第1项,
项目2;
私有字符串数组_微调器[];
专用字符串数组_喷丝头2[];
公共双费率[],
obj4;
公共字符串API_URL2,
新收费[],
s_查询;
公共数据库数据库;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.currency1);
数组_微调器=新字符串[38];
费率=新的双倍[38];
rates_new=新字符串[38];
数组_微调器[0]=“美元”;
比率[0]=1.0;
db=openOrCreateDatabase(“MyDB”,模式_PRIVATE,空);
execSQL(“如果存在FunnyNames,则删除表”);
execSQL(“如果不存在,则创建表FunnyNames(id INTEGER,Email DOUBLE,FirstName VARCHAR,LastName VARCHAR);”;
db.execSQL(“插入FunnyNames值('0','“+rates[0]+”,'Anita','Bath');”;
对于(int i=1;i arg0){}
});
最终喷丝器s2=(喷丝器)findViewById(R.id.spinner24);
ArrayAdapter适配器2=新的ArrayAdapter(此,
android.R.layout.simple_微调器(项目、数组微调器);
s2.设置适配器(适配器2);
adapter2.setDropDownViewResource(R.layout.spinner\U布局);
s2.setOnItemSelectedListener(新的OnItemSelectedListener(){
已选择公共视图(AdapterView<?>argo0,视图arg1,
整数arg2,长arg3){
int item=s2.getSelectedItemPosition();
项目2=项目;
}
未选择的公共无效(AdapterView<?>arg0){
});
按钮电流转换=(按钮)findViewById(R.id.Button12);
currency\u convert.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
最终编辑文本输入\货币=(编辑文本)findViewById(R.id.editText11);
最终文本视图转换为欧元=(文本视图)findViewById(R.id.textView61);
如果(!input_currency.getText().toString().equals(“”){
转换的\u currency.setText(String.format(“+rates\u new[6]+”“+rates\u new[20]+”“+rates\u new[31]);
}否则{
Toast.makeText(getApplicationContext(),“请输入美元值!”,Toast.LENGTH\u LONG.show();
}
}
});
}
}
您在数据库中得到了null
,因为您对Yahoo API的调用是异步的。在从Yahoo获得答案之前,您正在向数据库中插入数组。您必须将填充数据库的for循环的内容移动到AsyncHttpResponseHandler.onSuccess(字符串响应)
方法中。您在数据库中得到null
,因为您对Yahoo API的调用是异步的。在从Yahoo获得答案之前,您正在向数据库中插入数组。您必须将填充数据库的for循环的内容移动到AsyncHttpResponseHandler.onSuccess(字符串响应)
方法中。您在数据库中得到null
,因为您对Yahoo API的调用是异步的。在从Yahoo获得答案之前,您正在向数据库中插入数组。您必须将填充数据库的for循环的内容移动到AsyncHttpResponseHandler.onSuccess(字符串响应)
方法中。您在数据库中得到null
,因为您对Yahoo API的调用是异步的。在从Yahoo获得答案之前,您正在向数据库中插入数组。您必须将填充数据库的for循环的内容移动到AsyncHttpResponseHandler.onSuccess(String response)
方法中。您应该在此帖子中添加一个Android标记。您是在编译时还是在运行时收到错误?错误是什么?我没有得到错误,存储在数据库中的rates[k]的值为null。这意味着s_查询字符串无法读取并获取rates[k]的值。您应该为此帖子添加一个Android标记。您是在编译时还是在运行时收到错误?错误是什么?我没有得到错误,存储在数据库中的rates[k]的值为null。这意味着s_查询字符串无法读取并获取rates[k]的值。您应该为此帖子添加一个Android标记。您是在编译时还是在运行时收到错误?错误是什么?我没有得到错误,存储在数据库中的rates[k]的值为null。这意味着s_查询字符串无法读取并获取rates[k]的值。您应该为此帖子添加一个Android标记。您是在编译时还是在运行时收到错误?错误是什么?我没有得到错误,存储在数据库中的rates[k]的值为null。这意味着s_查询字符串无法读取并获取rates[k]的值。这将使obj4
也成为一个数组,而不是它想要的数组。同样,这与OP的问题无关。@TedHopp那么什么是obj4
?无论您希望它是什么,请在新行中定义它。它应该是一个双精度值,每次迭代都会更改它的值。在您最近的编辑中,这两个声明具有相同的效果。为什么你认为第一个是错误的?(关于它们为什么相同的解释,请参阅。)现在您刚刚教了我一个我不知道的Java语法。这将使