3

我有一个MainActivity有两个标签的ActionBar. 我对该活动的主要布局是一个空的FrameLayout. 第一个选项卡包含一个ProgressBar,第二个选项卡Button开始下载文件。这是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;

    }
}

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

当我单击StartDownload Button时,应用程序崩溃并出现以下异常:

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)

有谁知道如何更改ProgressBar应用程序不同部分的进度?

4

1 回答 1

1

您的代码中的主要问题,其原因NullPointerException是行:

imagesTab = new ImagesTab();

你在哪里实例化一个新的ImagesTab Fragment. 问题是这个新Fragment实例没有绑定到一个Activity(它在“空中”),所以它的getActivity()方法返回null. 此null值来自getActivity()您传递给Intent您在方法中构建的downloadStarted()方法,该方法将抛出NulPointerException. 与其实例化您所做的那样,您应该通过使用并根据其标签搜索片段来ImgesTab获取对已经(很可能)在父活动中的另一个片段的引用。getFragmentManager()

其次,更新ProgressBar似乎更像是一个应用程序设计问题。例如,为什么您Button在一个选项卡中开始下载,而ProgressBar在另一个选项卡中显示下载进度?此外,您似乎使用 a进行下载,因此您应该真正使下载文件的选项卡和所需的选项卡片段(带有)进行通信Service,而不是使选项卡进行通信。这可以通过在某些下载级别进行发送广播并让您的(包含两个选项卡片段)动态注册一个. 当发送广播时(例如在 10%),'s将接收它(如果它还活着),然后它会更新ServiceProgressBarServiceActivityBroadcastReceiverServiceActivityBroadcastReceiverProgressBarFragment如果Fragment在那一刻可见,则从所需的。

于 2012-11-26T15:08:27.937 回答