Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8687257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:04:31+00:00 2026-06-12T23:04:31+00:00

I have a button in an activity. When I press it, it will load

  • 0

I have a button in an activity. When I press it, it will load another activity. In this activity I have a custom grid view with a description and an image in each cell. I am populating the grid view from a remote server through an asynchronous task:

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        if (isInternetAvailable) {
            Intent intent = new Intent(menuActivity.this,
                    gridActivity.class);
            startActivity(intent);
        }
        else {
            MyAlertDialog.ShowAlertDialog(menuActivity.this,
                                          "",
                                          "No network connection",
                                          "OK");
        }
    }
});

I am sharing the testAsyncTask() here. I am calling this function in the onCreate() of gridActivity (the second activity)

private void testAsyncTask() {
    new AsyncTask<Object, Object, Object>() {

        @Override
        protected void onPreExecute() {

            progress_Dialog = ProgressDialog.show(a, "", "Loading");
        }

        @Override
        protected Integer doInBackground(Object... params) {
            try {
                MenuService menuService = new MenuServiceImpl();
                MenuServiceResponse pMenu = menuService.getMenu();
                itemlist = pMenu.getMenu().getMenus();
                return 0;
            }
            catch (MyServiceException e) {
                MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "No network connection detected.", "OK");
                e.printStackTrace();
            }
            return 0;
        }

        @Override
        protected void onPostExecute(Object result) {

            if (progress_Dialog != null) {
                progress_Dialog.dismiss();
            }

            try
            {
                adapter = new GridAdapter(
                    GridActivity.this, itemlist);
                AllitemgridView.setAdapter(adapter);

            }
            catch(Exception e)
            {
                adapter = null;
                MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "Check Network Connection", "OK");
            }

        }
    }.execute();
}

I am checking a condition in the first activity where I kept the button. I will fire the start activty only if I have an Internet connection.

Now in to my problem, if I have an Internet connection and I press the button, startacvitiy will fire, and it will move to the second activity. In the onCreate() of the second activity I’m calling the testAsync(). Now it will show a loading dialog box and in the background I will fetch the data from a remote server.

Now if I switch my modem off, the application freezes for 5-6 seconds and then it will force close. So how can I handle this exception?

The coding inside the doInbackgorund() requires an Internet connection and that could be the reason for this crash when I disconnect
from Wi-Fi. How can I handle this crash?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:04:32+00:00Added an answer on June 12, 2026 at 11:04 pm

    You can’t use this in doinbackground method, because this method is doing another thread,
    MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "No network connection detected.", "OK");

    You can only use it in the onpost method.

    Or you can use the dialog message:

        @Override
        protected Integer doInBackground(Object... params) {
            try {
                MenuService menuService = new MenuServiceImpl();
                MenuServiceResponse pMenu = menuService.getMenu();
                itemlist = pMenu.getMenu().getMenus();
                return 0;
            } catch (MyServiceException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "No network connection detected.", "OK");
                    }
                });
                e.printStackTrace();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a lot of buttons on main activity. Each button run this same
I have a list view in this activity First, i will display list view
hi I have a button which when (pressed) clicked will fire off another activity.
I have a phonegap activity which I want to start via a button press:
I have a button which on Click loads another activity, but before it loads
I want to make application that when press the button it will make each
So I have my main activity, and when you press a button it launches
I have an activity with two buttons, start and stop. If the user press
I am developing an activity which have an Update button as menu for android.
I have a activity which has a button and 2 autocomplete widget. for the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.