Android活动切换不正常

时间:2014-03-13 05:02:31

标签: android android-asynctask back-button illegalstateexception

当我从我的应用程序点击后退按钮时工作正常,但是当我从我的Android设备点击后退按钮并再次点击主页上的按钮时,我的应用就会停止。

主要活动 -

        public class MainActivity extends Activity implements LoginResponse  {

        LoginThread login;
        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                login = new LoginThread(this);
    login.delegate=this;

                       //function to execute when login button is pressed
                          login.execute();  // if login button is pressed
        }

        @Override
            public void processFinish(RequestToken output) {
if(output!=null)
        {
        try {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(output.getAuthenticationURL())));    
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("Error!!!", e.toString());
        }
        }
        else
        {
           Intent i= new Intent(this,UserClass.class);
           startActivity(i);
        }

        }

LoginThread类 -

public class LoginThread extends  AsyncTask<Void, Void, RequestToken>  {

public LoginThread(Context context)
    {
        this.context= context;
    }

protected void onPreExecute()
        {

            // Shared Preferences
            mSharedPreferences = context.getSharedPreferences("MyPref", 0);


            try{

            super.onPreExecute();

            pDialog = new ProgressDialog(context);
            pDialog.setMessage(Html.fromHtml("<b>Logging In</b><br/>Please wait..."));
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
            }
            catch (Exception ex)
            {
                Log.e("Error", "I got error", ex);
            }
        }


        protected RequestToken doInBackground(Void... urls) 
        {
            if(!isTwitterLoggedInAlready())
            {

            try 
            {   

                //Uri uri = ((Activity) context).getIntent().getData();
                Log.e("Hello","sff");
                Editor e= mSharedPreferences.edit();
                e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
                e.commit(); // save changes

                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
                builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
                Configuration configuration = builder.build();

                TwitterFactory factory = new TwitterFactory(configuration);
                twitter = factory.getInstance();
                return requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);


            } 
            catch (TwitterException ex) 
            {

                ex.printStackTrace();
                return null;
            }
          }

            else
            {


                return null;
            }

        }

        protected void onPostExecute(RequestToken result)
        {
                pDialog.dismiss();
                requestToken=result;
                delegate.processFinish(requestToken);

        }


        /**
         * Check user already logged in your application using twitter Login flag is
         * fetched from Shared Preferences
         * */
        private boolean isTwitterLoggedInAlready() {
            // return twitter login status from Shared Preferences
            return mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false);
        }

的logcat -

03-13 10:04:44.623: D/AndroidRuntime(17352): Shutting down VM
03-13 10:04:44.623: W/dalvikvm(17352): threadid=1: thread exiting with uncaught exception (group=0x41a4a700)
03-13 10:04:44.638: E/AndroidRuntime(17352): FATAL EXCEPTION: main
03-13 10:04:44.638: E/AndroidRuntime(17352): java.lang.IllegalStateException: Could not execute method of the activity
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.view.View$1.onClick(View.java:3838)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.view.View.performClick(View.java:4475)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.view.View$PerformClick.run(View.java:18784)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.os.Handler.handleCallback(Handler.java:730)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.os.Looper.loop(Looper.java:137)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.app.ActivityThread.main(ActivityThread.java:5414)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at java.lang.reflect.Method.invoke(Method.java:525)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at dalvik.system.NativeStart.main(Native Method)
03-13 10:04:44.638: E/AndroidRuntime(17352): Caused by: java.lang.reflect.InvocationTargetException
03-13 10:04:44.638: E/AndroidRuntime(17352):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at java.lang.reflect.Method.invoke(Method.java:525)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.view.View$1.onClick(View.java:3833)
03-13 10:04:44.638: E/AndroidRuntime(17352):    ... 11 more
03-13 10:04:44.638: E/AndroidRuntime(17352): Caused by: java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.os.AsyncTask.execute(AsyncTask.java:534)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at pack.locationinfo.MainActivity.TwitterLogin(MainActivity.java:75)
03-13 10:04:44.638: E/AndroidRuntime(17352):    ... 14 more

1 个答案:

答案 0 :(得分:0)

我得到了答案..感谢@Gaurav Gupta ..问题是我每次在主Activity类中都没有创建LoginThread类的新对象

这是主要活动类

的修改代码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            LoginThread login;
                    /**
                     *  Reference to this activity
                     */
                    login = new LoginThread(this);


                    /**
                     *  login interface
                     */
                    login.delegate=this;
                login.execute();
}

我错误地全局声明了LoginThread类的对象