gpt4 book ai didi

Android startActivity() 导致空指针异常

转载 作者:行者123 更新时间:2023-11-29 16:05:25 28 4
gpt4 key购买 nike

调用 startActivity 时,我一直收到 NPE,我认为这与我提供给 Intent 的上下文有关。当我从 Internet 获取 xml 数据时,我试图让启动画面保持不变。 splash Activity 创建一个启动 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 Activity :

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

最佳答案

如果您希望 SbplXmlData 成为一个 Activity ,您不能通过实例化它的新对象来启动它。您必须使用 Intent。

您确定要让 SbplXmlData 成为一个 Activity 而不仅仅是一个对象吗? (即跳过“扩展 Activity ”)

编辑:

只需将 AsyncTask 设为 SbplSplash 的子类,并将 loadPage() 设为上述 Activity 的成员函数即可。

关于Android startActivity() 导致空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19188372/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com