我正在解析从API检索的数据,然后在列表视图中显示它。我还包括了一个swipeToRefreshLayout。我希望列表每2秒后自动更新一次信息。
错误::尝试在空对象引用上调用虚方法'java.io.File android.content.Context.getCacheDir()' 这个错误就在这一行 -
RequestQueue requestQueue = Volley.newRequestQueue(this);
我对处理程序了解不多。我想我没有在正确的地方使用它。
以下是完整的代码:
public class BitbnsPrice extends AppCompatActivity {
private TextView mtv1;
private ListView list;
ArrayList<String> price=null;
private int mInterval = 5000;
private Handler mHandler;
private SwipeRefreshLayout swipe2refresh;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe2refresh= (SwipeRefreshLayout) findViewById(R.id.swipe2refresh);
swipe2refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipe2refresh.setRefreshing(true);
DataFromExchange();
swipe2refresh.setRefreshing(false);
}
});
DataFromExchange();
//ListView list=(ListView) findViewById(R.id.list);
}
public void updateList(ArrayList<String> price){
ListView list= (ListView) findViewById(R.id.list) ;
CoinAdapter adapter= new CoinAdapter(this,R.layout.coin_layout,price);
list.setAdapter(adapter);
}
public void DataFromExchange(){
String URL="https://bitbns.com/order/getTickerAll";
//////////below line is showing me the error after 3 second.
RequestQueue requestQueue= Volley.newRequestQueue(this);
JsonArrayRequest objreq = new JsonArrayRequest(
Request.Method.GET,
URL,
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
getDataFromExchange Getprice= new getDataFromExchange();
price= Getprice.getDataFromBitbnsExchange(response);
String str="";
for(String p : price)
str+=p + " ";
Log.e("price from API response" ,str);
//////////////////////////////////////////////////////
updateList(price);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error response" ,error.toString());
Toast.makeText(BitbnsPrice.this," Network error", Toast.LENGTH_LONG).show();
}
}
);
requestQueue.add(objreq);
final Handler handler = new Handler();
Runnable refresh = new Runnable() {
@Override
public void run() {
new BitbnsPrice().DataFromExchange();
handler.postDelayed(this, 3 * 1000);
}
};
handler.postDelayed(refresh, 3 * 1000);
}
}
答案 0 :(得分:0)
我将处理程序放在一个不同的函数中,然后我从onCreate方法调用它并且它有效。
这是更改的代码。这完全没问题。
private final Handler handler = new Handler();
private void doTheAutoRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
DataFromExchange();
doTheAutoRefresh();
}
}, 2000);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
doTheAutoRefresh(); //// added code
swipe2refresh= (SwipeRefreshLayout) findViewById(R.id.swipe2refresh);
swipe2refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipe2refresh.setRefreshing(true);
DataFromExchange();
swipe2refresh.setRefreshing(false);
}
});
DataFromExchange();
//ListView list=(ListView) findViewById(R.id.list);
}