3

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();

}
Ed R
  • 91
  • 1
  • 6
  • show your code please – sourabh devpura Jun 22 '15 at 23:31
  • Read the question properly or ask for something more specific. The issue is independent of my code. If you want an example activity that causes issues ask for that, but asking for 'my code' means very little as anything I write causes this issue. – Ed R Jun 22 '15 at 23:42
  • code where you setting break point . cos if the bug in your layout file (XML) it's impossible to find the bug by breakpoint . so now show your code and please tell witch kind of error you gating when onCreate() method is lode i am not asking for error for breakpoint – sourabh devpura Jun 22 '15 at 23:52
  • Okay I will edit and add some code. Can I just point out that it doesn't crash when there isn't a breakpoint set, I just have a bug that causes wrong behaviour I need to find. The error I'm getting from `onCreate()` isn't an error as such, the whole app just crashes. There is no Logcat Output or anything that could be used to find out what went wrong (as mentioned in question). The breakpoint doesn't even need to be in `onCreate()`, it can be anywhere in the app and it will still crash on load. It works without crashing when all breakpoints are removed. – Ed R Jun 23 '15 at 00:07
  • R u extends this activity from Activity class ? – sourabh devpura Jun 23 '15 at 00:55
  • It's extended from `ActionBarActivity` in the support library. – Ed R Jun 23 '15 at 00:58

2 Answers2

0

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.

asozcan
  • 1,370
  • 1
  • 17
  • 24
  • I have a feeling it might be my phone. Debugging on an AVM seems to work fine. I'll submit a bug report to Oneplus and see where it gets to and keep this updated as to what's happening. – Ed R Jun 24 '15 at 08:17
  • @EdR, I have the same bug on Emulator, so it looks like not related to specific phone. – Serhiy Jun 29 '17 at 08:26
  • The link is behind a login, can you cite the solution? – jornane Oct 19 '20 at 12:19
0

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

Ello
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 10 '22 at 22:10