首页 > 解决方案 > bundle.getParcelableArrayList() 返回 null

问题描述

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

这是我如何获取数据并将数据放入包中的代码:package 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();

    }

}

标签: javaandroidandroid-studio

解决方案


问题很可能不是尝试将数据放入的方式,Bundle而是您尝试读取数据的时间

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

这意味着Fragment在其onCreate()回调中被添加到Activity中。然后,FragmentonCreateView()被称为试图获取此时的ParcelableArray那个null。在这些操作之后,您的 HTTP 调用Activity完成并被onResponse()调用并将数据添加到包中。


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


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

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

1)从电话中删除MakeCandleStokChart()呼叫onCreateView()

2)在PutChartData's onResponse(),你应该找到你的Fragment使用FragmentManager检查这个线程)并candleEntries在其中设置然后调用MakeCandleStokChart()。它会是这样的:

HomeFragment fragment = (HomeFragment) supportFragmentManager.findFragmentById(R.id.replaceWithYourFragmentId);
fragment.candleEntries = candleData;
fragment.MakeCandleStokChart();

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

您可以在文档中阅读更多关于发出异步请求的信息(底部有课程)。关于 Fragment<->Activity 通信,请查看此文档页面


推荐阅读