无法开始活动

时间:2013-09-25 21:26:40

标签: java android android-intent asynchronous

我通过意图传递一个包并启动一个活动,但它没有执行。日志中的例外是

09-25 21:11:44.514: E/AndroidRuntime(426): FATAL EXCEPTION: main
09-25 21:11:44.514: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x.y.httpcommunicationhelper2/com.x.y..httpcommunicationhelper2.logout}: java.lang.NullPointerException.

你能否指出错误。你能帮忙吗?代码是:

//Intent part 
if (result != null) {
    Intent tokenIntent = new Intent(mContext, tokenActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("responsedata", result.substring(result.indexOf("=") + 1, result.length()));

    tokenIntent.putExtras(bundle);
    startActivity(tokenIntent);


    //logout class

    public class logout extends Activity {

        TextView logouttext;
        Bundle bundle2;
        Context mContext;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main3);

            mContext = this;
            logouttext = (TextView) findViewById(R.id.tvlogout);
            bundle2 = getIntent().getExtras();

            Log.i("logout - ats token", bundle2.getString("responsedata"));

            URLconnector ss = new URLconnector();
            ss.execute("url");

        }


        private class URLconnector extends AsyncTask < String, Void, String > {

            @Override
            protected String doInBackground(String...urls) {
                String response = "";
                for (String url: urls) {

                    DefaultHttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(url);
                    try {

                        List < NameValuePair > postParameters = new ArrayList < NameValuePair > ();

                        postParameters.add(new BasicNameValuePair("appID", "hjbhjbh"));
                        postParameters.add(new BasicNameValuePair("atsToken", bundle2.getString("responsedata")));

                        Log.i("logout - ats token", bundle2.getString("responsedata"));


                        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
                            postParameters);

                        httppost.setEntity(formEntity);


                        HttpResponse logoutresp = httpclient.execute(httppost);
                        Log.i("Logout response ", logoutresp.toString());

错误日志是:

09-25 21:11:44.514: E/AndroidRuntime(426): FATAL EXCEPTION: main
09-25 21:11:44.514: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x.y.httpcommunicationhelper2/com.x.y..httpcommunicationhelper2.logout}: java.lang.NullPointerException

09-25 21:11:44.514: E/AndroidRuntime(426):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-25 21:11:44.514: E/AndroidRuntime(426):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-25 21:11:44.514: E/AndroidRuntime(426):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-25 21:11:44.514: E/AndroidRuntime(426):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-25 21:11:44.514: E/AndroidRuntime(426):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 21:11:44.514: E/AndroidRuntime(426):  at android.os.Looper.loop(Looper.java:123)
09-25 21:11:44.514: E/AndroidRuntime(426):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-25 21:11:44.514: E/AndroidRuntime(426):  at java.lang.reflect.Method.invokeNative(Native Method)
09-25 21:11:44.514: E/AndroidRuntime(426):  at java.lang.reflect.Method.invoke(Method.java:507)
09-25 21:11:44.514: E/AndroidRuntime(426):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-25 21:11:44.514: E/AndroidRuntime(426):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-25 21:11:44.514: E/AndroidRuntime(426):  at dalvik.system.NativeStart.main(Native Method)
09-25 21:11:44.514: E/AndroidRuntime(426): Caused by: java.lang.NullPointerException
09-25 21:11:44.514: E/AndroidRuntime(426):  at com.x.y.httpcommunicationhelper2.logout.onCreate(logout.java:52)
09-25 21:11:44.514: E/AndroidRuntime(426):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-25 21:11:44.514: E/AndroidRuntime(426):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

1 个答案:

答案 0 :(得分:0)

更改行:

bundle2 = getIntent().getExtras();

Log.i("logout - ats token", bundle2.getString("responsedata"));

bundle2 = getIntent().getExtras();
if (bundle2  != null) {
       Log.i("logout - ats token", bundle2.getString("responsedata"));
}

您还需要相应地更改URLconnector的代码。