调用 startActivity 时,我不断收到 NPE,我认为这与我提供给 Intent 的上下文有关。我正在尝试在从 Internet 获取 xml 数据时保持启动画面。启动活动创建一个启动 AsyncTask 的 sbplXmlData 对象。onPostExecute() 就是调用 startActivity()。谢谢你的帮助。
public class SbplSplash extends Activity {
String now_playing, earned;
ArrayList<String> gameIdsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
/**
* Showing splashscreen while making network calls to download necessary
* data before launching the app Will use AsyncTask to make http call
*/
/* Invoke Xml Parser and database creator/population */
gameIdsList = new ArrayList<String>();
SbplXmlData xmlDataObj = new SbplXmlData(gameIdsList);
gameIdsList = xmlDataObj.getGameId();
}
}
SbplXmlData 活动:
public class SbplXmlData extends Activity {
SbplXmlData(ArrayList<String> gameIdsList) {
this.gameIdsList = gameIdsList;
loadPage();
}
public Context getContext() {
return this;
}
// Uses AsyncTask to download the XML feed
public void loadPage() {
/* assume we have a connection for now */
wifiConnected = true;
if ((wifiConnected || mobileConnected)) {
new DownloadXmlTask(this).execute(URL);
} else {
Log.e(TAG, "Unable to downlaod from URL");
// show error
}
}
// Implementation of AsyncTask used to download XML feed from nfl.com.
private class DownloadXmlTask extends AsyncTask<String, Void, String> {
Context context;
DownloadXmlTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... urls) {
try {
return loadXmlFromNetwork(urls[0]);
} catch (IOException e) {
return getResources().getString(R.string.connection_error);
} catch (XmlPullParserException e) {
return getResources().getString(R.string.xml_error);
}
}
@Override
protected void onPostExecute(String result) {
// do something to indicate success
super.onPostExecute(result);
// will close this activity and launch main activity
Intent i = new Intent(context, LiveViewActivity.class);
//i.putStringArrayListExtra("list", gameIdsList);
startActivity(i);
}
}
这是日志猫:
10-04 14:47:14.242: I/SbplXmlData(32559): 14
10-04 14:47:14.242: D/OpenGLRenderer(32559): Enabling debug mode 0
10-04 14:47:14.242: D/AndroidRuntime(32559): Shutting down VM
10-04 14:47:14.242: W/dalvikvm(32559): threadid=1: thread exiting with uncaught exception (group=0x42021ac8)
10-04 14:47:14.252: E/AndroidRuntime(32559): FATAL EXCEPTION: main
10-04 14:47:14.252: E/AndroidRuntime(32559): java.lang.NullPointerException
10-04 14:47:14.252: E/AndroidRuntime(32559): at android.app.Activity.startActivityForResult(Activity.java:3430)
10-04 14:47:14.252: E/AndroidRuntime(32559): at android.app.Activity.startActivityForResult(Activity.java:3391)
10-04 14:47:14.252: E/AndroidRuntime(32559): at android.app.Activity.startActivity(Activity.java:3626)
10-04 14:47:14.252: E/AndroidRuntime(32559): at android.app.Activity.startActivity(Activity.java:3594)
10-04 14:47:14.252: E/AndroidRuntime(32559): at com.jbrewsapps.sbpl.SbplXmlData$DownloadXmlTask.onPostExecute(SbplXmlData.java:103)
10-04 14:47:14.252: E/AndroidRuntime(32559): at com.jbrewsapps.sbpl.SbplXmlData$DownloadXmlTask.onPostExecute(SbplXmlData.java:1)
10-04 14:47:14.252: E/AndroidRuntime(32559): at android.os.AsyncTask.finish(AsyncTask.java:631)
10-04 14:47:14.252: E/AndroidRuntime(32559): at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-04 14:47:14.252: E/AndroidRuntime(32559): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-04 14:47:14.252: E/AndroidRuntime(32559): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 14:47:14.252: E/AndroidRuntime(32559): at android.os.Looper.loop(Looper.java:137)
10-04 14:47:14.252: E/AndroidRuntime(32559): at android.app.ActivityThread.main(ActivityThread.java:5328)
10-04 14:47:14.252: E/AndroidRuntime(32559): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 14:47:14.252: E/AndroidRuntime(32559): at java.lang.reflect.Method.invoke(Method.java:511)
10-04 14:47:14.252: E/AndroidRuntime(32559): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-04 14:47:14.252: E/AndroidRuntime(32559): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-04 14:47:14.252: E/AndroidRuntime(32559): at dalvik.system.NativeStart.main(Native Method)
10-04 14:47:14.282: I/Process(32559): Sending signal. PID: 32559 SIG: 9