Android startActivity()会导致nullpointerexception

时间:2013-10-04 18:33:43

标签: android android-intent nullpointerexception

我在调用startActivity时不断获取NPE,我认为它与我向Intent提供的上下文有关。当我从互联网上获取xml数据时,我正试图保持闪屏。 splash活动创建一个启动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);

        }
    }

这是logcat:

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

1 个答案:

答案 0 :(得分:4)

如果您希望SbplXmlData是一个活动,则无法通过实例化它的新对象来启动它。您必须使用Intent。

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

修改

只需将AsyncTask作为SbplSplash的子类,并将loadPage()作为上述活动的成员函数。