我有一个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
应用程序不同部分的进度?