开发者

android multiple activities starting for no reason

开发者 https://www.devze.com 2023-02-20 04:48 出处:网络
When i run a line of code to start an activity i get the following printout in the logcat and several of the same activity starting? can anyone help?

When i run a line of code to start an activity i get the following printout in the logcat and several of the same activity starting? can anyone help?

android multiple activities starting for no reason

My code to achieve this is...

public void login()
{
    System.out.println("1");
    SessionEvents.AuthListener listener = new SessionEvents.AuthListener()  {

        @Override
        public void onAuthSucceed() {
            facebookConnector.idnum();

            checkdatabase();

            if(id == ""){

            Intent i = new Intent(facebook.this, signup.class);
            startActivity(i);
            finish();
            }
            else{
                System.out.println("TEST");
                Intent i = new Intent(facebook.this, mainMenu.class);
                startActivity(i);
                finish();   

            }

        }

        @Override
        public void onAuthFail(String error) {
            System.out.println("2");

        }
    };
    System.out.println("3");
    SessionEvents.addAuthListener(listener);
    System.out.println("4");
    facebookConnector.login();
}

followed by the checkdatabase method...

public void checkdatabase(){


    fbid = FacebookConnector.id;
    System.out.println("facebook id in check database = " + fbid);
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


    nameValuePairs.add(new BasicNameValuePair("fbid",fbid));
    try{
        HttpClient httpclient = new DefaultHttpClient();


        HttpPost httppost = new HttpPost("checkdatabase.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity(); 
        is = entity.getContent();
        System.out.println("---------");
        System.out.println(is);
    }catch(Exception e) 
    { 
    Log.e("log_tag", "Error in http connection "+e.toString()); 
    } 

    //convert response to string 
    try 
    { 
        System.out.println("WSSSSSSSSSSSSSS");
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
    System.out.println(reader + "TEST");
    StringBuilder sb = new StringBuilder(); 
    System.out.println("Wmmmmmmmmmmmmmmm");
    String line = null; 
    while ((line = reader.readLine()) != null)  
    { 
    sb.append(line + "\n"); 
    } 
    is.close(); 
    result=sb.toString(); 
    System.out.print("SAD");
    System.out.println(result + "READER");

    JSONArray jArray = new JSONArray(result); 
    JSONObject json_data = jArray.getJSONObject(1);


    id = json_data.getString("facebookid");
    System.out.println("facebooooooook id: " + fbid);
    System.out.println("database id: " + id);
    } 
    catch(Exception e) 
    {   
    Log.e("log_tag", "Error converting result "+e.toString()); 
    } 
    //parse json data 

}

FacebookConnector Class

public class FacebookConnector {

private Facebook facebook = null;
private Context context;
private String[] permissions;
private Handler mHandler;
private Activity activity;
static String id;
private SessionListener mSessionListener = new SessionListener();;

public FacebookConnector(String appId,Activity activity,Context context,String[] permissions) {
    this.facebook = new Facebook(appId);
    id ="";

    SessionStore.restore(facebook, context);
    SessionEvents.addAuthListener(mSessionListener);
    SessionEvents.addLogoutListener(mSessionListener);

    this.context=context;
    this.permissions=permissions;
    this.mHandler = new Handler();
    this.activity=activity;
}

public void login() {
    System.out.println("5");
  //  if (!facebook.isSessionValid()) {
        facebook.authorize(this.activity, this.permissions,new LoginDi开发者_开发百科alogListener());
   // }
}

public void logout() {
    System.out.println("33");
    SessionEvents.onLogoutBegin();
    AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(this.facebook);
    asyncRunner.logout(this.context, new LogoutRequestListener());
}
public void idnum(){
    try {
        JSONObject jObject = null;
        try {
            jObject = new JSONObject(facebook.request("me"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    
        id =jObject.getString("id");

        System.out.println("facebook id: " + id);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void postMessageOnWall(String msg) {
    if (facebook.isSessionValid()) {
        Bundle parameters = new Bundle();
        parameters.putString("message", msg);
        try {
            String response = facebook.request("me/feed", parameters,"POST");
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        login();
    }
}   

private final class LoginDialogListener implements DialogListener {
    public void onComplete(Bundle values) {
        SessionEvents.onLoginSuccess();
    }

    public void onFacebookError(FacebookError error) {
        SessionEvents.onLoginError(error.getMessage());
    }

    public void onError(DialogError error) {
        SessionEvents.onLoginError(error.getMessage());
    }

    public void onCancel() {
        SessionEvents.onLoginError("Action Canceled");
    }
}

public class LogoutRequestListener extends BaseRequestListener {
    public void onComplete(String response, final Object state) {
        // callback should be run in the original thread, 
        // not the background thread
        mHandler.post(new Runnable() {
            public void run() {
                SessionEvents.onLogoutFinish();
            }
        });
    }
}

private class SessionListener implements AuthListener, LogoutListener {

    public void onAuthSucceed() {
        SessionStore.save(facebook, context);
    }

    public void onAuthFail(String error) {
    }

    public void onLogoutBegin() {           
    }

    public void onLogoutFinish() {
        SessionStore.clear(context);
    }
}

public Facebook getFacebook() {
    System.out.println("55");
    return this.facebook;
}

}


Your code is unclear, but if the top code is your "facebookConnector" class then each time you call login, you add the listener again, before recursively calling login again.


Add logs to Authenticate and make sure it's called only once. I'd start there...

0

精彩评论

暂无评论...
验证码 换一张
取 消