-3

I keep getting a Fatal Exception: main error when testing my application and i am unable to see where the issue lies

if anyone could spot were I have gone wrong in this it would be a great help

please find the code for my main page below:

public class DashboardActivity extends Activity {
UserFunctions userFunctions;
Button btnLogout;

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


     /**Dashboard Screen for the application * */       
         // Check login status in database
    userFunctions = new UserFunctions ();
    if(userFunctions.isUserLoggedIn(getApplicationContext())){
        // user already logged in show databoard
            btnLogout = (Button) findViewById(R.id.btnLogout);

        btnLogout.setOnClickListener(new View.OnClickListener() {

            public void onClick(View argo) {
                userFunctions.logoutUser(getApplicationContext());
                Intent login = new Intent (getApplicationContext(), LoginActivity.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(login);
                finish();
            }


        });

            }else{
                Intent login = new Intent (getApplicationContext(), LoginActivity.class);
                    login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(login);
                    finish();




    }
 }

}

} LogCat messages:

E/AndroidRuntime(21912): FATAL EXCEPTION: main
E/AndroidRuntime(21912): android.os.NetworkOnMainThreadException
E/AndroidRuntime(21912):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
E/AndroidRuntime(21912):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
E/AndroidRuntime(21912):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
E/AndroidRuntime(21912):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
E/AndroidRuntime(21912):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
E/AndroidRuntime(21912):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
E/AndroidRuntime(21912):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
E/AndroidRuntime(21912):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
E/AndroidRuntime(21912):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
E/AndroidRuntime(21912):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
E/AndroidRuntime(21912):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
E/AndroidRuntime(21912):    at com.applicationnfclogin.internal.JSONParser.getJSONFormUrl(JSONParser.java:85)
E/AndroidRuntime(21912):    at com.applicationnfclogin.internal.UserFunctions.loginUser(UserFunctions.java:33)
E/AndroidRuntime(21912):    at com.applicationnfclogin.LoginActivity$1.onClick(LoginActivity.java:84)
E/AndroidRuntime(21912):    at android.view.View.performClick(View.java:4475)
E/AndroidRuntime(21912):    at android.view.View$PerformClick.run(View.java:18786)
E/AndroidRuntime(21912):    at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime(21912):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(21912):    at android.os.Looper.loop(Looper.java:176)
E/AndroidRuntime(21912):    at android.app.ActivityThread.main(ActivityThread.java:5419)
E/AndroidRuntime(21912):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(21912):    at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(21912):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
E/AndroidRuntime(21912):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
E/AndroidRuntime(21912):    at dalvik.system.NativeStart.main(Native Method)
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63

4 Answers4

1

"This exception is thrown when an application attempts to perform a networking operation on its main thread". You should call asynctask. Look at this solution: How to fix android.os.NetworkOnMainThreadException?

Community
  • 1
  • 1
Unii
  • 1,587
  • 15
  • 33
0

you are performing network operation on main thread. which android does not allow to do. use asyntask to perform network operation.

Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
0

In Android, should avoid doing heavy jobs on the UI thread (also called the main thread) because at that point, your application will stop responding to user feedback (i.e. touching the screen). You can read more about threads and processes in Android here.

In the latest versions, Android has put a policy that does not allow doing network jobs on the main thread, so that your application is never stalled. In order to connect to the network, you can either create a new Thread, or create an AsyncTask which handles the switch between the background and foreground threads for you.

akalipetis
  • 938
  • 1
  • 7
  • 14
-1

this is the problem with strictmode.. you can use this

StrictMode.ThreadPolicy policy = new StrictMode.
                ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
Android
  • 388
  • 2
  • 3
  • 14