按钮工作正常(没有崩溃)但是在我做完后它会将信息写入ArrayList
ListView
(尚未使用)它开始崩溃。造成这种情况的原因是什么以及如何解决?
我添加的代码(不同的活动,但我通过startActivityForResult
使用按钮进行活动。它获取onActivityResult
中声明的信息,另一个函数放置所有信息进入ArrayList
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
if(resultCode == RESULT_OK){
String addName = data.getStringExtra("Name");
String addPrice = data.getStringExtra("Price");
String addAmmount = data.getStringExtra("Ammount");
boolean pressed = data.getBooleanExtra("Pressed", FALSE);
int ammount = Integer.parseInt(addAmmount);
int currentprice = Integer.parseInt(addPrice);
int intval = ammount * currentprice;
portfoliolist.add(new PortfolioItem(addName, ammount, intval, 0, 0, 0));
GetPrice();
}
if(resultCode == RESULT_CANCELED){
}
}
}
private void GetPrice(){
OkHttpClient client = new OkHttpClient();
String url = "https://api.coinmarketcap.com/v1/ticker/";
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
final String myResponce = response.body().string();
globalCoin.clear();
try {
JSONArray coins = new JSONArray(myResponce);
for(int i = 0; i < coins.length(); i++){
try {
JSONObject coin = coins.getJSONObject(i);
String name = coin.getString("name");
String price = coin.getString("price_usd");
String change = coin.getString("percent_change_24h");
globalCoin.add(new Coin(name, price, change));
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
for(int i = 0; i < globalCoin.size(); i++){
for(int ii = 0; ii < portfoliolist.size(); ii++){
if(globalCoin.get(i).getName() == portfoliolist.get(ii).name){
int gottenprice = Integer.parseInt(globalCoin.get(i).getPrice());
portfoliolist.get(ii).price = gottenprice;
portfoliolist.get(ii).curvalue = portfoliolist.get(ii).price * portfoliolist.get(ii).ammount;
portfoliolist.get(ii).change = portfoliolist.get(ii).curvalue / portfoliolist.get(ii).intvalue - 1;
}
}
}
}
编辑:logcat日志:
05-05 18:26:33.121 28991-28991/com.example.vartotojas.heycrypto E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vartotojas.heycrypto, PID: 28991
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.vartotojas.heycrypto/com.example.vartotojas.heycrypto.PortfolioActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4268)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.vartotojas.heycrypto.PortfolioActivity.onActivityResult(PortfolioActivity.java:113)
at android.app.Activity.dispatchActivityResult(Activity.java:7276)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
&#13;
答案 0 :(得分:1)
这是由NullPointerException引起的。在添加项目之前初始化您的portfolioList,如下所示:
portfolioList = new ArrayList<>();
答案 1 :(得分:0)
正如您在java.util.ArrayList.add(java.lang.Object)' on a null object reference
上看到的那样,您将获得空指针异常。
可能组合词是空的。或者检查哪个其他列表为空
答案 2 :(得分:0)
在OnActivityResult
方法中,行:
portfoliolist.add(new PortfolioItem(addName, ammount, intval, 0, 0, 0));
显示错误。由于列表portfoliolist
为null
。
您应该始终使用NullPointerException
支票或if-else
try catch
if(portfoliolist != null){
// add the item to list
}else{
// first instantiate and then add item to list
}
答案 3 :(得分:0)
您可以在java中使用对象而无需启动它。在您的代码中,您没有启动keyIsPressed
并且您正在其中添加项目。
只需用ArrayList
方法替换您的部分代码。
onActivityResult()