Mattias te Wierik
Mattias te Wierik

Reputation: 143

java.lang.IllegalStateException: Could not execute method of the activity, how to solve?

I am building a timetable app independently regarding school. I am now in the testing stage of my app and it just works fine on android version 2.3.6

However when I try to run it on a virtual device it's starts like normal.

However, after hitting a button which should execute the process to login, the apps returns the following errors:

01-05 00:52:23.180: E/AndroidRuntime(858): FATAL EXCEPTION: main
01-05 00:52:23.180: E/AndroidRuntime(858): java.lang.IllegalStateException: Could not execute method of the activity
01-05 00:52:23.180: E/AndroidRuntime(858):  at android.view.View$1.onClick(View.java:3597)
01-05 00:52:23.180: E/AndroidRuntime(858):  at android.view.View.performClick(View.java:4202)
01-05 00:52:23.180: E/AndroidRuntime(858):  at android.view.View$PerformClick.run(View.java:17340)
01-05 00:52:23.180: E/AndroidRuntime(858):  at android.os.Handler.handleCallback(Handler.java:725)
01-05 00:52:23.180: E/AndroidRuntime(858):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-05 00:52:23.180: E/AndroidRuntime(858):  at android.os.Looper.loop(Looper.java:137)
01-05 00:52:23.180: E/AndroidRuntime(858):  at android.app.ActivityThread.main(ActivityThread.java:5039)
01-05 00:52:23.180: E/AndroidRuntime(858):  at java.lang.reflect.Method.invokeNative(Native Method)
01-05 00:52:23.180: E/AndroidRuntime(858):  at java.lang.reflect.Method.invoke(Method.java:511)
01-05 00:52:23.180: E/AndroidRuntime(858):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-05 00:52:23.180: E/AndroidRuntime(858):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-05 00:52:23.180: E/AndroidRuntime(858):  at dalvik.system.NativeStart.main(Native Method)
01-05 00:52:23.180: E/AndroidRuntime(858): Caused by: java.lang.reflect.InvocationTargetException
01-05 00:52:23.180: E/AndroidRuntime(858):  at java.lang.reflect.Method.invokeNative(Native Method)
01-05 00:52:23.180: E/AndroidRuntime(858):  at java.lang.reflect.Method.invoke(Method.java:511)
01-05 00:52:23.180: E/AndroidRuntime(858):  at android.view.View$1.onClick(View.java:3592)
01-05 00:52:23.180: E/AndroidRuntime(858):  ... 11 more
01-05 00:52:23.180: E/AndroidRuntime(858): Caused by: android.os.NetworkOnMainThreadException
01-05 00:52:23.180: E/AndroidRuntime(858):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-05 00:52:23.180: E/AndroidRuntime(858):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-05 00:52:23.180: E/AndroidRuntime(858):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-05 00:52:23.180: E/AndroidRuntime(858):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-05 00:52:23.180: E/AndroidRuntime(858):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-05 00:52:23.180: E/AndroidRuntime(858):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-05 00:52:23.180: E/AndroidRuntime(858):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-05 00:52:23.180: E/AndroidRuntime(858):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-05 00:52:23.180: E/AndroidRuntime(858):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-05 00:52:23.180: E/AndroidRuntime(858):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-05 00:52:23.180: E/AndroidRuntime(858):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-05 00:52:23.180: E/AndroidRuntime(858):  at com.mattle.timetable.JSONParser.getJSONFromUrl(JSONParser.java:42)
01-05 00:52:23.180: E/AndroidRuntime(858):  at com.mattle.timetable.LoginView.doLogin(LoginView.java:129)
01-05 00:52:23.180: E/AndroidRuntime(858):  ... 14 more

I use the jsonparser class from this page: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

My login code looks like this:

package com.mattle.timetable;

import static com.mattle.timetable.MainActivity.PREFS_NAME;

import java.io.BufferedReader;
import java.math.BigInteger;
import java.net.URI;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.google.android.gcm.GCMRegistrar;
import com.markupartist.android.widget.ActionBar;

public class LoginView extends Activity {

private JSONParser jsonParser;

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

    ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
    // You can also assign the title programmatically by passing a
    // CharSequence or resource id.
    actionBar.setTitle("Jouw Lesrooster");
}

@Override
public void onBackPressed() {

    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);

}

 private String getStringResourceByName(String aString)
    {
      String packageName = "com.mattle.timetable";
      int resId = getResources().getIdentifier(aString, "string", packageName);
      return getString(resId);
    }

private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}

public String sha1(String s) {
    MessageDigest digest = null;
            try {
                    digest = MessageDigest.getInstance("SHA-1");
            } catch (NoSuchAlgorithmException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
    digest.reset();
    byte[] data = digest.digest(s.getBytes());
    return String.format("%0" + (data.length*2) + "X", new BigInteger(1, data));
}

public void doRegister(View view) {

    Intent gotoregister = new Intent(this, Register.class);
    startActivity(gotoregister);

}

public void doLogin(View view) throws Exception {

    BufferedReader in = null;

    try{

        if(haveNetworkConnection()) {

            EditText getusername = (EditText) findViewById(R.id.username);
            String username = getusername.getText().toString();

            EditText getpassword = (EditText) findViewById(R.id.password);
            String password = sha1(getpassword.getText().toString() + "SOMESALTHERENVM");

        HttpClient client = new DefaultHttpClient();
        URI website = new URI("SOMEURL");
        HttpPost request = new HttpPost();
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("login_username", username));
        nameValuePairs.add(new BasicNameValuePair("login_password", password));

        jsonParser = new JSONParser();
        JSONObject json = jsonParser.getJSONFromUrl("http://jouwlesrooster.nl/api/doLogin/", nameValuePairs);

        try {

            if (json.getString("success") != null) {
                String res = json.getString("success");
                if(Integer.parseInt(res) == 1){
            //Log.d("MYTAG", "Print this in logcat...");
            //Log.d("MYTAG", username + " ddd " + password);

            getSharedPreferences("myLoginshit",MODE_PRIVATE)
            .edit()
            .putString("Username", username)
            .putString("Password", password)
            .commit();

            GCMRegistrar.checkDevice(this);
            GCMRegistrar.checkManifest(this);
            String regId = GCMRegistrar.getRegistrationId(this);
            if (regId.equals("")) {
              GCMRegistrar.register(this, "***");
              Log.i("test", "ddd");
             // Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG).show();
            } else {
            //  Toast.makeText(getApplicationContext(), "Already registered", Toast.LENGTH_LONG).show();
             // Log.v(TAG, "Already registered");
            }

            Intent gotomenu = new Intent(this, Menu.class);
            startActivity(gotomenu);

            }
            }

        } finally {

        }

        } else {


        }

    }finally{
        if (in != null) {
            try{
                in.close();

            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
}

How can I solve this annoying error?

Thanks Mattle

Upvotes: 0

Views: 3417

Answers (3)

AndroidLearner
AndroidLearner

Reputation: 4636

  • Its always better practice to have the UI work on UI-Thread and Non-UI work on Non-UI Thread, but from HoneyComb version of Android it became a Law and won't allowed any network operation in the UI thread

  • In your case doLogin() is a blocking call, which you are calling from UI thread, where as it should be on the Non-UI thread.

  • To get rid of this use AsyncTask or Thread.

Upvotes: 1

Orab&#238;g
Orab&#238;g

Reputation: 11992

It looks like your executing your doLogin() method from the UI-Thread (possibly some onClick method in a button).

You should not do that, because Network access are potentially slow and could freeze the UI. Use the AsyncTask class instead (see http://developer.android.com/reference/android/os/AsyncTask.html)

Upvotes: 1

Lukas Knuth
Lukas Knuth

Reputation: 25755

The problem is the doLogin()-method, which performs Network tasks on the UI-thread.

Looking at your StackTrace you can see:

Caused by: android.os.NetworkOnMainThreadException

this is thrown since Android "Ice Cream Sandwich". To get rid of it, put the Network code in an AsyncTask.

See this older question: How to fix android.os.NetworkOnMainThreadException?

Upvotes: 1

Related Questions