抱歉,我对 Android 还很陌生,所以请多多包涵。我正在尝试设置一个在加载 JSON 时显示的启动画面。当我启动应用程序时,启动画面第一次显示几秒钟,然后崩溃。之后每当我尝试启动该应用程序时,它都会崩溃,甚至没有显示启动画面。
从 BaseActivity.java 中检索 JSONTask 类
public class RetreiveJSONTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
Log.d("RetrieveJSONTask", "Begin fetching text.");
return fetchJSON(urls[0]);
}
protected void onPostExecute(String JSON) {
}
FetchJSON 方法:
public String fetchJSON(String URL) {
StringBuilder builder = new StringBuilder();
AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
HttpGet httpGet = new HttpGet(URL);
// Download JSON file from LocalResearch
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
// Write line-by-line to StringBuilder
while ((line = reader.readLine()) != null)
{
Log.d(this.toString(), line);
builder.append(line);
}
} else {
Log.e(this.toString(), URL );
Log.e(this.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (client != null)
client.close();
return builder.toString();
}
SplashActivity onCreate 方法:
public static ArrayList<Item> downloadedItems = new ArrayList<Item>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new RetreiveJSONTask() {
protected void onPostExecute(String JSON)
{
downloadedItems = parseJSON ( JSON );
Intent i = new Intent(SplashActivity.this, MagazinePagesActivity.class);
startActivity(i);
// Closes the splash screen
finish();
}
}.execute(sourceURL());
}
第一次崩溃的错误日志:
10-15 19:43:13.504: D/RetrieveJSONTask(769): Begin fetching text.
10-15 19:43:13.673: V/SlidingMenu(769): setting padding!
10-15 19:43:13.744: D/libEGL(769): loaded /system/lib/egl/libEGL_emulation.so
10-15 19:43:13.754: D/(769): HostConnection::get() New Host Connection established 0x2a0ef958, tid 769
10-15 19:43:13.793: D/libEGL(769): loaded /system/lib/egl/libGLESv1_CM_emulation.so
10-15 19:43:13.804: D/libEGL(769): loaded /system/lib/egl/libGLESv2_emulation.so
10-15 19:43:13.874: W/EGL_emulation(769): eglSurfaceAttrib not implemented
10-15 19:43:13.884: D/OpenGLRenderer(769): Enabling debug mode 0
10-15 19:43:13.984: V/CustomViewBehind(769): behind INVISIBLE
10-15 19:43:13.984: W/View(769): requestLayout() improperly called by android.widget.ListView{417003e0 VFED.VC. ......ID 0,0-366,738 #102000a android:id/list} during layout: running second layout pass
10-15 19:43:15.644: W/dalvikvm(769): threadid=11: thread exiting with uncaught exception (group=0x41465700)
10-15 19:43:15.794: E/AndroidRuntime(769): FATAL EXCEPTION: AsyncTask #1
10-15 19:43:15.794: E/AndroidRuntime(769): java.lang.RuntimeException: An error occured while executing doInBackground()
10-15 19:43:15.794: E/AndroidRuntime(769): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-15 19:43:15.794: E/AndroidRuntime(769): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-15 19:43:15.794: E/AndroidRuntime(769): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-15 19:43:15.794: E/AndroidRuntime(769): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-15 19:43:15.794: E/AndroidRuntime(769): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-15 19:43:15.794: E/AndroidRuntime(769): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-15 19:43:15.794: E/AndroidRuntime(769): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-15 19:43:15.794: E/AndroidRuntime(769): at java.lang.Thread.run(Thread.java:841)
10-15 19:43:15.794: E/AndroidRuntime(769): Caused by: java.lang.NullPointerException
10-15 19:43:15.794: E/AndroidRuntime(769): at java.net.URI.parseURI(URI.java:353)
10-15 19:43:15.794: E/AndroidRuntime(769): at java.net.URI.<init>(URI.java:204)
10-15 19:43:15.794: E/AndroidRuntime(769): at java.net.URI.create(URI.java:725)
10-15 19:43:15.794: E/AndroidRuntime(769): at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
10-15 19:43:15.794: E/AndroidRuntime(769): at com.dailybruin.bruinframework.base.BaseActivity.fetchJSON(BaseActivity.java:112)
10-15 19:43:15.794: E/AndroidRuntime(769): at com.dailybruin.bruinframework.base.BaseActivity$RetreiveJSONTask.doInBackground(BaseActivity.java:163)
10-15 19:43:15.794: E/AndroidRuntime(769): at com.dailybruin.bruinframework.base.BaseActivity$RetreiveJSONTask.doInBackground(BaseActivity.java:1)
10-15 19:43:15.794: E/AndroidRuntime(769): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-15 19:43:15.794: E/AndroidRuntime(769): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-15 19:43:15.794: E/AndroidRuntime(769): ... 4 more
提前致谢!