我所要做的就是将我的TextViews更改为当前价格,并以S& P 500的百分比进行更改。出于某种原因,我的应用程序仍在崩溃。我是碎片的新手,所以我不确定我是否将代码放在正确的位置。有人有想法吗?任何帮助将不胜感激。
这是我的代码:
package com.capstone.hammond.wallstreetfantasyleaguefinal;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.apache.http.util.ByteArrayBuffer;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
public class CurrentMarketTrendsFragment extends Fragment {
TextView spPrice;
TextView spPercent;
View rootview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_current_market_trends, container, false);
return rootview;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
spPrice = (TextView) view.findViewById(R.id.sp_price);
spPercent = (TextView) view.findViewById(R.id.sp_percent);
getQuotes();
}
public void getQuotes() {
try {
final String spSymbol = "^GSPC";
List<String> results = new YahooFinanceTask().execute(spSymbol).get();
setResult(results.get(0), results.get(1), results.get(2));
} catch(InterruptedException e) {
e.printStackTrace();
} catch(ExecutionException e) {
e.printStackTrace();
}
}
private void setResult(String fStockSymbol, String stockPrice, String fStockChangePercentage) {
spPrice.setText(stockPrice);
spPercent.setText(fStockChangePercentage);
}
public class YahooFinanceTask extends AsyncTask<String, Void, List<String>> {
@Override
protected List<String> doInBackground(String... symbols) {
try {
URL url;
try {
url = new URL("http://download.finance.yahoo.com/quotes.csv?s=" + symbols[0] + "&f=nl1p2");
InputStream stream = url.openStream();
BufferedInputStream bis = new BufferedInputStream(stream);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
String stockTxt = new String(baf.toByteArray());
String[] tokens = stockTxt.split(",");
String stockSymbol = tokens[0];
String stockPrice = tokens[1];
String stockChangePercentage = tokens[2];
String fStockSymbol = stockSymbol.substring(1,
stockSymbol.length() - 1);
String fStockChangePercentage = stockChangePercentage
.substring(1, stockChangePercentage.length() - 3);
List<String> results = new ArrayList<String>();
results.add(fStockSymbol);
results.add(stockPrice);
results.add(fStockChangePercentage);
return results;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
}
这是我的logcat输出:
04-20 13:44:46.779: E/AndroidRuntime(3213): FATAL EXCEPTION: main
04-20 13:44:46.779: E/AndroidRuntime(3213): Process: com.capstone.hammond.wallstreetfantasyleaguefinal, PID: 3213
04-20 13:44:46.779: E/AndroidRuntime(3213): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.capstone.hammond.wallstreetfantasyleaguefinal/com.capstone.hammond.wallstreetfantasyleaguefinal.NavDrawer}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.os.Looper.loop(Looper.java:135)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-20 13:44:46.779: E/AndroidRuntime(3213): at java.lang.reflect.Method.invoke(Native Method)
04-20 13:44:46.779: E/AndroidRuntime(3213): at java.lang.reflect.Method.invoke(Method.java:372)
04-20 13:44:46.779: E/AndroidRuntime(3213): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-20 13:44:46.779: E/AndroidRuntime(3213): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-20 13:44:46.779: E/AndroidRuntime(3213): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
04-20 13:44:46.779: E/AndroidRuntime(3213): at com.capstone.hammond.wallstreetfantasyleaguefinal.CurrentMarketTrendsFragment.getQuotes(CurrentMarketTrendsFragment.java:52)
04-20 13:44:46.779: E/AndroidRuntime(3213): at com.capstone.hammond.wallstreetfantasyleaguefinal.CurrentMarketTrendsFragment.onViewCreated(CurrentMarketTrendsFragment.java:44)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:971)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.app.Activity.performStart(Activity.java:5949)
04-20 13:44:46.779: E/AndroidRuntime(3213): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
04-20 13:44:46.779: E/AndroidRuntime(3213): ... 10 more
04-20 13:44:47.055: I/art(3213): Background sticky concurrent mark sweep GC freed 5528(543KB) AllocSpace objects, 2(32KB) LOS objects, 13% free, 4MB/4MB, paused 46.922ms total 268.858ms
04-20 13:44:47.254: W/ActivityManager(1246): Force finishing activity com.capstone.hammond.wallstreetfantasyleaguefinal/.NavDrawer
04-20 13:44:47.271: E/gralloc_goldfish(932): gralloc_alloc: Mismatched usage flags: 576 x 576, usage 333
04-20 13:44:47.271: W/GraphicBufferAllocator(932): alloc(576, 576, 1, 00000333, ...) failed -22 (Invalid argument)
04-20 13:44:47.271: E/(932): GraphicBufferAlloc::createGraphicBuffer(w=576, h=576) failed (Invalid argument), handle=0x0
04-20 13:44:47.278: E/BufferQueueProducer(1246): [ScreenshotClient] dequeueBuffer: createGraphicBuffer failed
04-20 13:44:47.280: W/WindowManager(1246): Screenshot failure taking screenshot for (1080x1920) to layer 22015
04-20 13:44:47.280: E/ActivityManager(1246): Invalid thumbnail dimensions: 576x576
04-20 13:44:47.506: I/OpenGLRenderer(1246): Initialized EGL, version 1.4
04-20 13:44:47.607: W/EGL_emulation(1246): eglSurfaceAttrib not implemented
04-20 13:44:47.608: W/OpenGLRenderer(1246): Failed to set EGL_SWAP_BEHAVIOR on surface 0x9e6ff120, error=EGL_SUCCESS
04-20 13:44:49.461: I/Choreographer(1246): Skipped 102 frames! The application may be doing too much work on its main thread.
04-20 13:44:49.465: D/TaskPersister(1246): removeObsoleteFile: deleting file=465_task.xml
04-20 13:44:49.581: W/ActivityManager(1246): Activity pause timeout for ActivityRecord{2ead84f2 u0 com.capstone.hammond.wallstreetfantasyleaguefinal/.NavDrawer t466 f}
04-20 13:44:49.871: W/EGL_emulation(1519): eglSurfaceAttrib not implemented
04-20 13:44:49.871: W/OpenGLRenderer(1519): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3650b40, error=EGL_SUCCESS
04-20 13:44:52.559: W/OpenGLRenderer(1519): Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
04-20 13:44:52.559: W/OpenGLRenderer(1519): Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
04-20 13:44:52.559: W/OpenGLRenderer(1519): Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
04-20 13:45:00.044: I/ProcessStatsService(1246): Prepared write state in 4ms
04-20 13:45:02.220: W/ActivityManager(1246): Activity destroy timeout for ActivityRecord{2ead84f2 u0 com.capstone.hammond.wallstreetfantasyleaguefinal/.NavDrawer t466 f}
04-20 13:45:02.304: W/ActivityManager(1246): Activity destroy timeout for ActivityRecord{2fb205b6 u0 com.capstone.hammond.wallstreetfantasyleaguefinal/.LoginActivity t465 f}
04-20 13:45:02.348: W/SurfaceFlinger(932): couldn't log to binary event log: overflow.
04-20 13:45:05.745: D/TaskPersister(1246): removeObsoleteFile: deleting file=466_task.xml
所以它是由空指针异常引起的,但我不知道它为什么会这样。
答案 0 :(得分:0)
我不认为您可以直接访问doInBackground返回的内容,而不是在此处尝试
List<String> results = new YahooFinanceTask().execute(spSymbol).get();
您需要覆盖AsynsTask中的onPostExecute
并在那里调用setResult
方法。
@Override
protected void onPostExecute(List<String> results) {
super.onPostExecute(results);
setResult(results.get(0), results.get(1), results.get(2));
}
然后在onViewCreated
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
spPrice = (TextView) view.findViewById(R.id.sp_price);
spPercent = (TextView) view.findViewById(R.id.sp_percent);
final String spSymbol = "^GSPC";
new YahooFinanceTask().execute(spSymbol);
}