在我的Android应用程序的 Fragment 中,我希望在价格,公司名称,股票代码等的帮助下,将 Yahoo Finance 的简单数据显示出来 csv (以逗号分隔的值)。当我尝试在FinanceFragment中将值设置为TextView时,我将Error作为NullPointerException。任何人都可以告诉我正确的方法或修复我的代码。
感谢您的帮助!
Activity.java
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_search_ticker, null);
builder.setView(dialogView);
final EditText input = (EditText) dialogView.findViewById(R.id.input_ticker);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setPositiveButton("SEARCH TICKER", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String SearchTicker = input.getText().toString();
if(SearchTicker.matches("")) { //Check if EditText is empty / If empty show message
Toast.makeText(getApplicationContext(), "Space is empty. Please try enter again!", Toast.LENGTH_SHORT).show();
}else{
if(new YahooAsyncTask().execute(SearchTicker) == null){
Toast.makeText(getApplicationContext(), "There is no such ticker. Please try enter again!", Toast.LENGTH_SHORT).show();
}else{
new YahooAsyncTask().execute(SearchTicker);
}
}
}
});
private class YahooAsyncTask extends AsyncTask<String, String, Stock> {
LinearLayout Progress = (LinearLayout) findViewById(R.id.Progress);
LinearLayout NoInternetConnection = (LinearLayout) findViewById(R.id.NoInternetConnection);
FrameLayout FragmentContainer = (FrameLayout) findViewById(R.id.FragmentContainer);
@Override
protected void onPreExecute() {
// Sets company info invisible, and progress indicator visible
Progress.setVisibility(View.VISIBLE);
NoInternetConnection.setVisibility(View.INVISIBLE);
FragmentContainer.setVisibility(View.GONE);
}
protected Stock doInBackground(String... ticker) {
Stock stock = null;
if (isNetworkAvailable()) {
try {
stock = YahooFinanceInfo.getStockData(ticker[0]);
} catch (Exception e) {
Log.d("YAHOO_ERROR", e.toString());
}
}
return stock;
}
protected void onPostExecute(Stock stock) {
Progress.setVisibility(View.GONE);
if (stock != null) {
// Create a new Fragment to be placed in the activity layout
YahooFragment yahooFragment = YahooFragment.newInstance(stock);
if (findViewById(R.id.FragmentContainer) != null) {
getSupportFragmentManager().beginTransaction().replace(R.id.FragmentContainer, yahooFragment).commit();
} else {
getSupportFragmentManager().beginTransaction().add(R.id.FragmentContainer, yahooFragment).commit();
}
FragmentContainer.setVisibility(View.VISIBLE);
} else {
NoInternetConnection.setVisibility(View.VISIBLE);
}
}
}
答案 0 :(得分:0)
请发布错误代码以确定,但我猜这是发生在这里:
LinearLayout HeaderProgress = (LinearLayout) getView().findViewById(R.id.progress_linear_layout);
LinearLayout NoInternetConnection = (LinearLayout) getView().findViewById(R.id.NoInternetConnection);
LinearLayout FragmentContainer = (LinearLayout) getView().findViewById(R.id.fragment_container);
此代码将在您返回onCreateView()之前执行,这意味着getView()可能返回null。
尝试将这些属性提取到您的活动并在调用之前初始化它们
new GetKeyStatsAsyncTask().execute(ticker);
<强>更新强>
这就是我在评论中的意思:
public class FinanceFragment extends Fragment {
TextView tvCompany;
TextView tvCompanyTicker;
TextView tvPrice;
TextView tvPriceChange;
TextView tvPrevClose;
TextView tvOpen;
TextView tvVolume;
TextView tvAvgDailyVolume;
TextView tvDaysRange;
TextView tvYearRange;
TextView tvOneYearTarget;
TextView tvMarketCap;
Stock stock;
String ticker;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_finance, container, false);
tvCompany = (TextView) view.findViewById(R.id.tvCompany);
tvCompanyTicker = (TextView) view.findViewById(R.id.tvCompanyTicker);
tvPrice = (TextView) view.findViewById(R.id.tvPrice);
tvPriceChange = (TextView) view.findViewById(R.id.tvPriceChange);
tvPrevClose = (TextView) view.findViewById(R.id.tvPrevClose);
tvOpen = (TextView) view.findViewById(R.id.tvOpen);
tvAvgDailyVolume = (TextView) view.findViewById(R.id.tvAvgDailyVolume);
tvVolume = (TextView) view.findViewById(R.id.tvVolume);
tvDaysRange = (TextView) view.findViewById(R.id.tvDaysRange);
tvYearRange = (TextView) view.findViewById(R.id.tvYearRange);
tvMarketCap = (TextView) view.findViewById(R.id.tvMarketCap);
tvOneYearTarget = (TextView) view.findViewById(R.id.tvOneYearTarget);
updateView();
// When application start working, search for Google ticker by default.
ticker = "GOOG";
new GetKeyStatsAsyncTask().execute(ticker);
return view;
}
private void updateView() {
if(stock == null) {
// Sets company name, price, etc.
tvCompany.setText("");
tvCompanyTicker.setText("");
tvPrice.setText("");
tvPriceChange.setText("");
tvPrevClose.setText("");
tvOpen.setText("");
tvAvgDailyVolume.setText("");
tvVolume.setText("");
tvDaysRange.setText("");
tvYearRange.setText("");
tvMarketCap.setText("");
tvOneYearTarget.setText("");
} else {
// Sets company name, price, etc.
tvCompany.setText(stock.getName());
tvCompanyTicker.setText(stock.getTicker());
tvPrice.setText(stock.getPrice());
tvPriceChange.setText(stock.getPriceChange());
tvPrevClose.setText(stock.getPrevClose());
tvOpen.setText(stock.getOpen());
tvAvgDailyVolume.setText(stock.getAvgDailyVolume());
tvVolume.setText(stock.getVolume());
tvDaysRange.setText(stock.getDaysRange());
tvYearRange.setText(stock.getYearRange());
tvMarketCap.setText(stock.getMarketCap());
tvOneYearTarget.setText(stock.getOneYearTarget());
// If price change is positive, set TextView to green. Otherwise Red.
if (stock.getPriceChangeDirection() >= 0)
tvPriceChange.setTextColor( getResources().getColor(R.color.finance_green));
else
tvPriceChange.setTextColor( getResources().getColor(R.color.finance_red));
}
}
...
并更改你的onPostExecute:
protected void onPostExecute(Stock stock) {
HeaderProgress.setVisibility(View.GONE);
if (stock != null) {
FragmentContainer.setVisibility(View.VISIBLE);
updateView();
} else {
NoInternetConnection.setVisibility(View.VISIBLE);
}
}
我没有尝试代码,因此无法保证其有效