gpt4 book ai didi

java - bundle.getParcelableArrayList() 返回 null

转载 作者:行者123 更新时间:2023-12-02 08:54:24 26 4
gpt4 key购买 nike

我正在尝试从 bundle 中提取蜡烛条目的 ArrayList。但是在收到越界异常后,我意识到返回的数组是空的。我对 Android 编程非常陌生,仍在尝试解决问题。谢谢

这是我的代码,说明如何获取数据并将数据放入包中: 包 com.example.bullfinance;

import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.github.mikephil.charting.data.CandleEntry;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.ActionBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;


public class stockinfo extends AppCompatActivity {

public String url = "";
public Bundle bundle;
public String tickerSymbol;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stockinfo);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
/* AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_News, R.id.navigation_PatternIndicators, R.id.navigation_SpecialStats)
.build();*/
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
//NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);

bundle = getIntent().getExtras();
tickerSymbol = bundle.getString("STOCKTICKER");



setToolBarTitle();
PutChartData();




}

public void setToolBarTitle()
{
Toolbar toolbar = findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

ActionBar theToolbar = getSupportActionBar();

theToolbar.setDisplayHomeAsUpEnabled(true);

url = "https://financialmodelingprep.com/api/v3/company/profile/" + this.getIntent().getExtras().getString("STOCKTICKER");

JsonObjectRequest toolBarTitleRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try
{
ActionBar nameRequestToolbar = getSupportActionBar();

JSONObject myJsonObject = response.getJSONObject("profile");

nameRequestToolbar.setTitle(myJsonObject.getString("companyName"));

} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {

}
});

NetworkBridge.getInstance(this).addToRequestQueue(toolBarTitleRequest);
}

public void PutChartData()
{
String chartUrl = "https://financialmodelingprep.com/api/v3/historical-price-full/"+ this.getIntent().getExtras().getString("STOCKTICKER") + "?from=0000-00-00&to=" + GetCurrentDate();

Log.w("StockInfoActivity", chartUrl);

JsonObjectRequest chartDataRequest = new JsonObjectRequest(Request.Method.GET, chartUrl, null, new Response.Listener<JSONObject>() {

ArrayList<CandleEntry> candleData = new ArrayList<CandleEntry>();

@Override
public void onResponse(JSONObject response) {
try {
JSONArray historicalArray = response.getJSONArray("historical");

for(int i = 0; i < historicalArray.length(); i++)
{
candleData.add(new CandleEntry(i, historicalArray.getJSONObject(i).getInt("high"), historicalArray.getJSONObject(i).getInt("low"), historicalArray.getJSONObject(i).getInt("open"), historicalArray.getJSONObject(i).getInt("close")));
}

bundle.putParcelableArrayList("ChartRawData" ,candleData);

} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {

}
});

NetworkBridge.getInstance(this).addToRequestQueue(chartDataRequest);
}

public String GetCurrentDate()
{
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
return dateFormat.format(calendar.getTime());
}

}

这是我尝试提取 ArrayList 的代码:

package com.example.bullfinance.ui.home;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.bullfinance.NetworkBridge;
import com.example.bullfinance.R;
import com.example.bullfinance.stockinfo;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.CandleStickChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.ChartHighlighter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;


import android.os.Handler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Set;

public class HomeFragment extends Fragment {


public TextView thePrice;

public Bundle bundle;

public String tickerSymbol;

public String url;

public String chartUrl;

static public CandleStickChart theChart;

Handler handler = new Handler();

public Button todayBTN, fiveDayBTN, twoWeeksBTN, monthBTN, sixMonthBTN, lstQtrBTN, oneYearBTN, ytdBtn, allTimeBTN;

public Button intOneDayBTN, intFiveDayBTN, intMonthBTN, intSixMonthBTN, intOneYearBTN;

public Button selectedTimeFrameBTN;

public Button selectedIntervalBTN;

public ScrollView intervalScrollView;

public ArrayList<CandleEntry> candleEntries = new ArrayList<CandleEntry>();


public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View root = inflater.inflate(R.layout.fragment_home, container, false);

thePrice = (TextView) root.findViewById(R.id.priceText);

theChart = root.findViewById(R.id.chart);

bundle = getActivity().getIntent().getExtras();

tickerSymbol = bundle.getString("STOCKTICKER");

url = "https://financialmodelingprep.com/api/v3/stock/real-time-price/" + tickerSymbol;

candleEntries = bundle.getParcelableArrayList("ChartRawData");


todayBTN = root.findViewById(R.id.todaybtn);
fiveDayBTN = root.findViewById(R.id.fivedaybtn);
twoWeeksBTN = root.findViewById(R.id.monthbtn);
monthBTN = root.findViewById(R.id.monthbtn);
sixMonthBTN = root.findViewById(R.id.sixmonthbtn);
lstQtrBTN = root.findViewById(R.id.lstqtrBtn);
oneYearBTN = root.findViewById(R.id.oneyearbtn);
ytdBtn = root.findViewById(R.id.ytdbtn);
allTimeBTN = root.findViewById(R.id.alltimebtn);


intOneDayBTN = root.findViewById(R.id.intonedaybtn);
intFiveDayBTN = root.findViewById(R.id.intfivedaybtn);
intMonthBTN = root.findViewById(R.id.intmonthbtn);
intSixMonthBTN = root.findViewById(R.id.intsixmonthbtn);
intOneYearBTN = root.findViewById(R.id.intoneyearbtn);

//intervalScrollView = root.findViewById(R.id.intervalScrollView);

SetStockPrice();
handler.postDelayed(periodicUpdate, 5 * 1000);

theChart.setTouchEnabled(true);
theChart.setDragEnabled(true);
theChart.setScaleEnabled(true);
theChart.setPinchZoom(true);
theChart.setDoubleTapToZoomEnabled(true);
theChart.getDescription().setEnabled(false);
theChart.setHighlightPerDragEnabled(true);

theChart.animateXY(3000, 3000);

MakeCandleStockChart();

return root;

}


public void SetStockPrice() {


JsonObjectRequest getStockPriceRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

Double thePriceObt;

@Override
public void onResponse(JSONObject response) {
try {
thePriceObt = response.getDouble("price");

thePrice.setText(thePriceObt.toString());

} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {

}
});

NetworkBridge.getInstance(getContext()).addToRequestQueue(getStockPriceRequest);
}

Runnable periodicUpdate = new Runnable() {
@Override
public void run() {
handler.postDelayed(periodicUpdate, 4 * 1000);

SetStockPrice();
}
};

public void MakeCandleStockChart() {

CandleDataSet chartSet = new CandleDataSet(candleEntries, tickerSymbol);

chartSet.setDrawIcons(false);
chartSet.setAxisDependency(YAxis.AxisDependency.LEFT);
chartSet.setShadowColor(Color.BLACK);
chartSet.setShadowWidth(0.7f);
chartSet.setDecreasingColor(Color.RED);
chartSet.setDecreasingPaintStyle(Paint.Style.FILL);
chartSet.setIncreasingColor(Color.rgb(122, 242, 84));
chartSet.setIncreasingPaintStyle(Paint.Style.FILL);
chartSet.setNeutralColor(Color.BLUE);
chartSet.setHighlightEnabled(true);
chartSet.setHighLightColor(Color.BLACK);

CandleData data = new CandleData(chartSet);

theChart.setData(data);

theChart.invalidate();

}

}

最佳答案

问题很可能不是您如何尝试将数据放入Bundle,而是何时您尝试读取数据它。

您发布的代码 fragment 没有显示如何将 HomeFragment 添加到 Activity,但我的猜测是它是在 .xml 布局中完成的.

这意味着 Fragment 在其 onCreate() 回调中添加到 Activity 中。然后,调用Fragment的onCreateView()来尝试获取此时为nullParcelableArray。在这些操作之后,Activity 中的 HTTP 调用完成,并且调用 onResponse() 并将数据添加到包中。

<小时/>

如果您想检查我的理论,请在 PutChartData() 中的 onResponse() 和 Fragment 的 onCreateView() 中放置一个调试器断点>。我敢打赌 onCreateView() 首先被调用。

<小时/>

解决这个问题的方法是认识到使用 Volley 库(以及大多数其他 Android 网络库)发出的 HTTP 请求是异步的 - 它们不会阻塞主线程,而是会被执行在单独的一个任务中,在执行长时间运行的任务后,它们通过 onResponse() 回调将结果返回到主线程。

您应该使用此回调来“通知”应用程序中感兴趣的部分有关网络调用的结果。对于您的情况,最简单的解决方案可能应该是:

1) 从 onCreateView() 中删除 MakeCandleStokChart() 调用

2) 在 PutChartData 的 onResponse() 中,您应该使用 FragmentManager 找到您的 Fragment ( check this thread )并在其中设置 candleEntries,然后调用 MakeCandleStokChart()。它会是这样的:

HomeFragment fragment = (HomeFragment) supportFragmentManager.findFragmentById(R.id.replaceWithYourFragmentId);
fragment.candleEntries = candleData;
fragment.MakeCandleStokChart();
<小时/>

请记住,上面的解决方案并不是总体上最好的模式,但它应该可以帮助您理解某些事情是如何工作的。

您可以阅读有关发出异步请求 in the docs 的更多信息(底部有类(class))。关于Fragment<->Activity通信检查this documentation page

关于java - bundle.getParcelableArrayList() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60575342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com