1

I am trying to dev my app using the Parse.com lib. When a user login into his account, he goes to the Main activity. At the first time he does that, it is OK. But if he does a logout and then a login again, when he goes to the main activity shows up the error.

I am using a sliding menu that if the user is logged it shows the options: Settings and Logout. Or, shows Login and Sign Up.

Maybe the error is when I am setting the visibilitys of these views. Here is the logCat.

04-17 17:28:03.155: E/AndroidRuntime(14343): FATAL EXCEPTION: main
04-17 17:28:03.155: E/AndroidRuntime(14343): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.fitness.legacy.personal/br.com.activities.fitness.legacy.TelaPrincipalLogado}: android.os.NetworkOnMainThreadException
04-17 17:28:03.155: E/AndroidRuntime(14343): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
04-17 17:28:03.155: E/AndroidRuntime(14343): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
04-17 17:28:03.155: E/AndroidRuntime(14343): at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-17 17:28:03.155: E/AndroidRuntime(14343): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
04-17 17:28:03.155: E/AndroidRuntime(14343): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 17:28:03.155: E/AndroidRuntime(14343): at android.os.Looper.loop(Looper.java:137)
04-17 17:28:03.155: E/AndroidRuntime(14343): at android.app.ActivityThread.main(ActivityThread.java:4507)
04-17 17:28:03.155: E/AndroidRuntime(14343): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 17:28:03.155: E/AndroidRuntime(14343): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 17:28:03.155: E/AndroidRuntime(14343): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
04-17 17:28:03.155: E/AndroidRuntime(14343): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-17 17:28:03.155: E/AndroidRuntime(14343): at dalvik.system.NativeStart.main(Native Method)
04-17 17:28:03.155: E/AndroidRuntime(14343): Caused by: android.os.NetworkOnMainThreadException
04-17 17:28:03.155: E/AndroidRuntime(14343): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-17 17:28:03.155: E/AndroidRuntime(14343): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java:922)
04-17 17:28:03.155: E/AndroidRuntime(14343): at org.apache.http.impl.SocketHttpClientConnection.close(SocketHttpClientConnection.java:205)
04-17 17:28:03.155: E/AndroidRuntime(14343): at org.apache.http.impl.conn.DefaultClientConnection.close(DefaultClientConnection.java:161)
04-17 17:28:03.155: E/AndroidRuntime(14343): at org.apache.http.impl.conn.tsccm.AbstractConnPool.closeConnection(AbstractConnPool.java:320)
04-17 17:28:03.155: E/AndroidRuntime(14343): at org.apache.http.impl.conn.tsccm.AbstractConnPool.shutdown(AbstractConnPool.java:296)
04-17 17:28:03.155: E/AndroidRuntime(14343): at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.shutdown(ConnPoolByRoute.java:670)
04-17 17:28:03.155: E/AndroidRuntime(14343): at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.shutdown(ThreadSafeClientConnManager.java:257)
04-17 17:28:03.155: E/AndroidRuntime(14343): at com.parse.ParseRequest.initialize(ParseRequest.java:106)
04-17 17:28:03.155: E/AndroidRuntime(14343): at com.parse.Parse.initialize(Parse.java:108)
04-17 17:28:03.155: E/AndroidRuntime(14343): at br.com.activities.fitness.legacy.TelaPrincipalLogado.onCreate(TelaPrincipalLogado.java:65)
04-17 17:28:03.155: E/AndroidRuntime(14343): at android.app.Activity.performCreate(Activity.java:4465)
04-17 17:28:03.155: E/AndroidRuntime(14343): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
04-17 17:28:03.155: E/AndroidRuntime(14343): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
04-17 17:28:03.155: E/AndroidRuntime(14343): ... 11 more

Sorry for my bad english :/

UPDATE:

I realized that I am already using threads for the Login, Logout and Sign Up. Because the lib provides me the methods below:

ParseUser.logInInBackground(usuario, senha, new LogInCallback() {
              public void done(ParseUser user, ParseException e) {
                if (user != null) {
                    Intent it = new Intent(getApplicationContext(), TelaPrincipal.class);
                    finish();
                    startActivity(it);
                } else {
                    Log.e("Erro no login", e.getMessage()); 
                    Toast.makeText(getBaseContext(), getString(R.string.tente_novamente), Toast.LENGTH_LONG).show();
                }


              }
            });

Signup method

ParseUser user = new ParseUser();
            user.setUsername(usuario);
            user.setPassword(senha);
            user.setEmail(email);

            user.signUpInBackground(new SignUpCallback() {
                  public void done(ParseException e) {
                    if (e == null) {
                            startActivity(new Intent(getApplicationContext(), TelaPrincipal.class)); 
                    } else {
                        Log.e("Erro no Cadastro", e.getMessage());
                        textViewErro.setText(getString(R.string.tente_novamente));
                        textViewErro.setVisibility(View.VISIBLE);
                    }
                  }
                });

So, if the lib do these things to me throws a thread. What I need to do? When a user do loggin ou signup for the first time, everything runs OK. But if he logout and login again the exception shows up. I do not know what I have to do ;/

lucasbraum
  • 53
  • 2
  • 7

1 Answers1

3

It has been caused by the android.os.NetworkOnMainThreadException. You cannot do any network operations on the main UI thread in android. You need to do any network operations on a background thread. The easiest way for this is most likely a AsyncTask. You should read the documentation on threads here.

Kieron Papps
  • 145
  • 10
  • Please, Can you see the updates in question? thx – lucasbraum Apr 18 '14 at 00:12
  • It's very difficult to debug the code if it relies on a 3rd Party package. Since i won't know what it is doing. Also if the thread handling is not done by you that could also be tricky. It may be possible just to encapsulate the methods you think are throwing the error inside another thread. Also if my answer is correct to your first question, it would be great if you could 'tick' it as the correct answer. – Kieron Papps Apr 18 '14 at 00:17