我有一个小应用程序,显示当前的黄金和白银价格。我附上了应用程序的小屏幕截图。它有两个操作栏项目刷新和设置。当我点击刷新按钮时,我必须获得黄金的当前更新价格。我确实得到了一段时间的更新价格,但一段时间后它崩溃了以上错误。
我正在使用JSOUP库从UOB银行获取数据。
http://s23.postimg.org/580ofx68b/UOBGoldand_Silver.jpg
错误日志:
2779-2779/com.example.coherendz.uobgoldandsilver E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 26, size is 26
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.coherendz.uobgoldandsilver.ListViewAdapter.getView(ListViewAdapter.java:68)
at android.widget.AbsListView.obtainView(AbsListView.java:2161)
at android.widget.ListView.makeAndAddView(ListView.java:1840)
at android.widget.ListView.fillDown(ListView.java:675)
at android.widget.ListView.fillGap(ListView.java:639)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4970)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4127)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:531)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
我在下面发布我的代码:
MainActivity.java
package com.example.coherendz.uobgoldandsilver;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
ListView listview;
ListViewAdapter adapter;
String cadena;
private final String siteUrl = "https://uniservices1.uobgroup.com/secure/online_rates/gold_and_silver_prices.jsp";
private static String TAG = MainActivity.class.getSimpleName();
private TextView txtResponse;
private Document doc;
ArrayList<String> arr_linkText= new ArrayList<String>();
private ArrayList names,currency,weight,Buy,Sell;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtResponse = (TextView) findViewById(R.id.txtResponse);
(new ParseURL()).execute(new String[]{siteUrl});
}
private class ParseURL extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try
{
doc = Jsoup.connect(siteUrl).get();
String title = doc.title();
Log.i(TAG, title);
} catch (IOException e) {
e.printStackTrace();
}
names = Values(0);
currency = Values(1);
weight = Values(2);
Buy = Values(3);
Sell = Values(4);
for (Element table : doc.select("table")) {
for (Element row : table.select("tr.roll2,tr.roll1")) {
Elements column = row.select("td");
cadena = column.text();
cadena += "\n";
arr_linkText.add(cadena);
Log.i(TAG, cadena);
}
}
return arr_linkText.toString();
}
@Override
protected void onPostExecute(String s) {
listview = (ListView) findViewById(R.id.listView);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(arr_linkText,names,currency,weight,Buy,Sell,MainActivity.this);
// Set the adapter to the ListView
listview.setAdapter(adapter);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu_settings; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
Toast.makeText(MainActivity.this, "Settings Selected", Toast.LENGTH_LONG).show();
Intent i = new Intent(this,Settings.class);
startActivity(i);
}
else if(id == R.id.action_refresh){
Toast.makeText(MainActivity.this,"Refresh Selected",Toast.LENGTH_LONG).show();
(new ParseURL()).execute(new String[]{siteUrl});
}
return super.onOptionsItemSelected(item);
}
private ArrayList Values(int i) {
ArrayList Temparray = new ArrayList();
for (Element table : doc.select("table")) {
for (Element row : table.select("tr.roll2,tr.roll1")) {
Elements column = row.select("td");
cadena = column.get(i).text();
cadena += "\n";
Temparray.add(cadena);
//Log.i(TAG, cadena);
}
}
return Temparray;
}
}
ListViewAdapter.java
package com.example.coherendz.uobgoldandsilver;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ListViewAdapter extends BaseAdapter {
ArrayList myList = new ArrayList();
LayoutInflater inflater;
Context context;
private ArrayList currency,weight,Buy,Sell;
ArrayList<String> names= new ArrayList<String>();
public ListViewAdapter(ArrayList myList,ArrayList names,ArrayList currency,ArrayList weight,ArrayList Buy,ArrayList Sell, Context context) {
this.myList = myList;
this.names = names;
this.currency = currency;
this.weight = weight;
this.Buy = Buy;
this.Sell = Sell;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return myList.size();
}
@Override
public Object getItem(int position) {
return myList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder=null;
if(convertView == null) {
convertView = inflater.inflate(R.layout.custom_row, null);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
String name="",weig="",buy="",sell="";
name = names.get(position).toString();
weig = weight.get(position).toString();
buy = Buy.get(position).toString();
sell = Sell.get(position).toString();
mViewHolder.tvTitle.setText(name);
mViewHolder.tvQty.setText(weig);
mViewHolder.tvBuy.setText(buy);
mViewHolder.tvSell.setText(sell);
loadSavedPreferences();
return convertView;
}
private void loadSavedPreferences() {
SharedPreferences prefs;
prefs = context.getSharedPreferences("Checked Values", Context.MODE_PRIVATE);
String Arbor = prefs.getString("Arbor", null);
String Cast = prefs.getString("Cast", null);
if(Arbor!=null&&Cast!=null){
Log.i("SRI",Arbor);
Log.i("Kanth",Cast);
}
}
private class MyViewHolder {
TextView tvTitle,tvQty,tvBuy,tvSell;
MyViewHolder(View v){
tvTitle = (TextView) v.findViewById(R.id.tvName);
tvQty = (TextView) v.findViewById(R.id.tvQty);
tvBuy = (TextView) v.findViewById(R.id.tvBuy);
tvSell = (TextView) v.findViewById(R.id.tvSell);
}
}
}
任何建议都会有所帮助。
答案 0 :(得分:0)
该列表会为您提供myList.size()
个条目,但您在.get(position)
和names
以及适配器weight
中使用getView()
。
解决方案1:
检查构造函数中输入的长度,如果输入参数names
和weight
的长度小于myList
,则抛出异常。
解决方案2:
让getCount()
返回您在getView()
故事的寓意是绝不相信你的意见