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 8891045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:38:21+00:00 2026-06-14T22:38:21+00:00

I’ve got a MainActivity which has two tabs on ActionBar . My main layout

  • 0

I’ve got a MainActivity which has two tabs on ActionBar. My main layout on that activity is an empty FrameLayout. The first tab contains a ProgressBar, the second has Button that starts downloading files. Here’s SettingsTab:

public class SettingsTab extends Fragment {
    private Button downloadButton;
    private ImagesTab imagesTab;
    private static String fileDir = "sdcard/AndroidCourse/";
    public void downloadImages(String url,int limit,String imageExtension)
    {

        try {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectAll()
            .build());
            Document doc = Jsoup.connect(url).get();
            StringBuilder sb = new StringBuilder("img[src$=.");
            sb.append(imageExtension);
            sb.append("]");
            Elements images = doc.select(sb.toString());
            ArrayList<String> urls = new ArrayList<String>();
            for(Element image : images) {
                if(urls.size() == limit) {  
                    break;
                }
                urls.add(image.attr("src"));
            }
            Log.e("urls", String.valueOf(urls.size()));
            imagesTab.downloadStarted(urls);


        } catch (IOException e) {
            Log.e("SettingsTab", e.getMessage());
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.settings_tab, null);
        downloadButton = (Button) view.findViewById(R.id.startButton);
        imagesTab = new ImagesTab();
        downloadButton.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                downloadImages("http://www.google.com",5,"jpg");


            }

        });
        return view;

    }
}

and ImagesTab:

public class ImagesTab extends Fragment {
    ProgressBar downloadProgressBar;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.images_tab, null);
        downloadProgressBar = (ProgressBar) view.findViewById(R.id.downloadProgressBar);
        //downloadProgressBar.setVisibility(View.INVISIBLE);

        return view;

    }
    public void downloadStarted(ArrayList<String> urls)
    {
        //downloadProgressBar.setVisibility(View.VISIBLE);
        Intent intent = new Intent(getActivity(), DownloadService.class);
        intent.putExtra("urls", urls);
        intent.putExtra("receiver", new DownloadReceiver(new Handler()));
        getActivity().startService(intent);
        Toast.makeText(getActivity(), "Service started!" + urls.size(), Toast.LENGTH_SHORT).show();
    }
    private class DownloadReceiver extends ResultReceiver{
        public DownloadReceiver(Handler handler) {
            super(handler);
        }

        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            super.onReceiveResult(resultCode, resultData);
            if (resultCode == DownloadService.UPDATE_PROGRESS) {
                int progress = resultData.getInt("progress");
                downloadProgressBar.setProgress(progress);
                if (progress == 100) {
                    downloadProgressBar.setVisibility(View.INVISIBLE);
                }
            }
        }
    }
}

When I click the StartDownload Button, the application crashes with the following exception:

11-26 03:55:55.765: E/AndroidRuntime(21200): FATAL EXCEPTION: main
11-26 03:55:55.765: E/AndroidRuntime(21200): java.lang.NullPointerException
11-26 03:55:55.765: E/AndroidRuntime(21200):    at android.content.ComponentName.<init>(ComponentName.java:75)
11-26 03:55:55.765: E/AndroidRuntime(21200):    at android.content.Intent.<init>(Intent.java:3227)
11-26 03:55:55.765: E/AndroidRuntime(21200):    at com.example.lista2.ImagesTab.downloadStarted(ImagesTab.java:32)
11-26 03:55:55.765: E/AndroidRuntime(21200):    at com.example.lista2.SettingsTab.downloadImages(SettingsTab.java:55)
11-26 03:55:55.765: E/AndroidRuntime(21200):    at com.example.lista2.SettingsTab$1.onClick(SettingsTab.java:72)

Does anyone have an idea how to change the progress of a ProgressBar in on different part of app?

  • 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-14T22:38:22+00:00Added an answer on June 14, 2026 at 10:38 pm

    The main problem in your code, and the reason for that NullPointerException is the line:

    imagesTab = new ImagesTab();
    

    where you instantiate a new ImagesTab Fragment. The problem is this new Fragment instance isn’t tied to an Activity(it’s in the “air”) so its getActivity() method returns null. This null value from the getActivity() method you pass to the Intent that you build in the downloadStarted() method which will throw the NulPointerException. Instead of instantiating the ImgesTab like you do, you should get a reference to the other fragment which is already(most likely) in the parent activity by using the getFragmentManager() and searching for the fragment based on its tag.

    Second, updating the ProgressBar seems more of a app design problem. For example, why do you have the Button that starts the download in a tab and the ProgressBar which shows the download progress in another tab? Also, you seem to use a Service for the download, so instead of making the tabs communicating you should really make the Service that downloads the files and the desired tab fragment(with the ProgressBar) communicate. This could be made very simple by making the Service to a send broadcast at certain download levels and making your Activity(which holds the two tabs fragments) dynamically registering a BroadcastReceiver. When the Service sends a broadcast(for example at 10%), the Activity‘s BroadcastReceiver will pick it up(if it’s alive) and then it will update the ProgressBar from the desired Fragment if that Fragment is visible at that moment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have an array which has BIG numbers and small numbers in it. I
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I

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.