Android activity crashes when a breakpoint is set

2.4k views Asked by At

I have a strange issue I'm experiencing when developing an app for Android. I'm trying to debug my application using my phone (a Oneplus One), Android Studio and ADB. Everything works as normal except that when I start an activity that already has a breakpoint set in Android Studio, it crashes with no log output. This happens both when the activity is the launch activity and when I'm starting one with an intent.

As you can imagine, this is incredibly irritating since I have a bug in my onCreate() method that I want to break in.

I have tried adding the line Debug.waitForDebugger() to the bit of code above the breakpoints but this doesn't help.

I haven't added any code to this question because this error is independent of the code. It happens with multiple projects and Activities. I don't have another Android phone to test it on so can't check that. Has anyone else experienced this and/or found a fix?

Thanks in advance.

EDIT: Added an example of code. This code does not crash when a breakpoint is not set, neither does the breakpoint need to be in this bit of code for the crash to occur. This is just the code I'm trying to debug, behaviour of which would be impossible to fix without knowledge of my server back-end, so I'm not asking for help with that.

protected void onCreate(Bundle savedInstanceState) {
    //Create activity
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Check for play services
    if (!checkPlayServices()){
        //Exit and notify user that play services are required
        Toast.makeText(this, "Google Play Services must be installed for Insty to work", Toast.LENGTH_LONG).show();
        System.exit(1);
        }

    //Get token. If null, push to login screen
    SharedPreferences sp = getBaseContext().getSharedPreferences(NetService.SP_APP, Context.MODE_PRIVATE);
    if (!sp.contains(NetService.SP_TOKEN)){
        //Token doesn't exist - push to login screen
        startActivity(new Intent(MainActivity.this, LoginActivity.class));
        MainActivity.this.finish();
        return;
    }

    //Setup action bar
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    //Populate navigation drawer
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mDrawerList = (ListView)findViewById(R.id.left_drawer);
    String[] menuItems = getResources().getStringArray(R.array.menu_items);
    mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, menuItems));
    //Set an on click listener
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    //Add to action bar
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open_drawer, R.string.close_drawer){
        @Override
        public void onDrawerClosed(View view){
            invalidateOptionsMenu();
        }

        @Override
        public void onDrawerOpened(View view){
            invalidateOptionsMenu();
        }
    };

    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });

    //Start a token verification
    ResultReceiver receiver = new ResultReceiver(new Handler()){
        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            VerifyResult result = VerifyResult.values()[resultData.getInt(NetService.RESULT_VERIFY)];
            switch (result){
                case VALID:
                    //Do nothing
                    break;

                case INVALID:
                    //Push back to login screen
                    startActivity(new Intent(MainActivity.this, LoginActivity.class));
                    MainActivity.this.finish();
                    break;

                case BAD_NETWORK:
                    //Toast to let user know network is down
                    Toast.makeText(MainActivity.this, R.string.bad_network, Toast.LENGTH_LONG).show();
                    break;

                case ERROR:
                    //Toast to show error
                    Toast.makeText(MainActivity.this, R.string.generic_error, Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    //Start verify
    NetService.startActionVerify(this, receiver, sp.getString(NetService.SP_TOKEN, ""));

    //Open on chats fragment
    FragmentManager fragMgr = getFragmentManager();
    fragMgr.beginTransaction().replace(R.id.content_frame, new ChatsFragment()).commit();

}
2

There are 2 answers

3
asozcan On

Last versions(1.1-1.2) of Android Studio have some debugging bugs probably cause of new inline debugging features. There is an issue topic and a solution inside it. Give it a try, it solved my debugging problems.

1
Ello On

Test on another device with newer SDK. Had the same problem on Nexus 5X API 27 while on Pixel XL API 32 works fine.