我有一个带有AsyncTask子类 FileDownloaderAsync 的Activity类 FileDownloader 。 要更新我的MainActivity-ProgressDialog,我想在 updateProgress -function中发送一个意图,该意图由 onProgressUpdate -Event调用。
但是如果 Intent in = new Intent(this,ActMain.class); ,我每次都会得到 java.lang.NullPointerException 。
这是我的代码片段:
public class FileDownloader extends Activity {
...
public void updateProgress(int progressVal) {
Bundle bdl = new Bundle();
bdl.putInt("DownloadProgress", progressVal);
Intent in = new Intent(this, ActMain.class);
in.putExtras(bdl);
startActivity(in);
}
class FileDownloaderAsync extends AsyncTask<String, String, String> {
...
protected void onProgressUpdate(String... progress){
Log.d("ANDRO_ASYNC", progress[0]);
updateProgress(Integer.parseInt(progress[0]));
}
}
}
这里还有Logcat-Output:
`02-16 10:29:52.255: W/dalvikvm(540): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
02-16 10:29:52.265: E/AndroidRuntime(540): FATAL EXCEPTION: main
02-16 10:29:52.265: E/AndroidRuntime(540): java.lang.NullPointerException
02-16 10:29:52.265: E/AndroidRuntime(540): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
02-16 10:29:52.265: E/AndroidRuntime(540): at android.content.ComponentName.<init>(ComponentName.java:75)
02-16 10:29:52.265: E/AndroidRuntime(540): at android.content.Intent.<init>(Intent.java:3004)
02-16 10:29:52.265: E/AndroidRuntime(540): at de.tachoron.lwplayer.FileDownloader.updateProgress(FileDownloader.java:40)
02-16 10:29:52.265: E/AndroidRuntime(540): at de.tachoron.lwplayer.FileDownloader$FileDownloaderAsync.onProgressUpdate(FileDownloader.java:94)
02-16 10:29:52.265: E/AndroidRuntime(540): at de.tachoron.lwplayer.FileDownloader$FileDownloaderAsync.onProgressUpdate(FileDownloader.java:1)
02-16 10:29:52.265: E/AndroidRuntime(540): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:618)
02-16 10:29:52.265: E/AndroidRuntime(540): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 10:29:52.265: E/AndroidRuntime(540): at android.os.Looper.loop(Looper.java:137)
02-16 10:29:52.265: E/AndroidRuntime(540): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-16 10:29:52.265: E/AndroidRuntime(540): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 10:29:52.265: E/AndroidRuntime(540): at java.lang.reflect.Method.invoke(Method.java:511)
02-16 10:29:52.265: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-16 10:29:52.265: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-16 10:29:52.265: E/AndroidRuntime(540): at dalvik.system.NativeStart.main(Native Method)´
答案 0 :(得分:0)
这是更新mainactivity progressBar的错误方法。首先,我将解释为什么你会得到空指针,然后我将向你展示我在我的项目中实现的方式。
NullPointer问题:
updateProgress
从onProgressUpdate
多次调用updateProgress
。现在,当第一次进行progressUpdated时,onProgressUpdate
首次从startActivity(in);
调用updateProgress
。这意味着ActMain
正在FileDownloader
中执行pause
并启动updateProgress
。因此,此时您的onProgressUpdate
活动处于updateProgress
州。因此,当下次更新进度时,再次从ActMain
调用FileDownloader's context
,FileDownloader
尝试使用BroadCast
启动FileDownloader
活动。但是你的MainActivity
处于暂停状态,这就是你得到nullPointer异常的原因。
更新mainActivity progressBar的方法:
这是使用MainActivity
的最佳方式。别担心,这并不复杂。我完全表现出来了:
在您的public static final String ACTION_DOWNLOAD_ON_GOING = "downloading";
public static final String PROGRESS = "progress";
public static final String ACTION_DOWNLOADMANEGAR = "signeture_of_downloadmanegar";
public static final String ACTION_HINT = "hint";
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Broadcast the download progress to MainActivity
broadCast(ACTION_DOWNLOAD_ON_GOING, values[0]);
}
private void broadCast(String hint, int proggress){
Intent i = new Intent();
i.setAction(ACTION_DOWNLOADMANEGAR);
i.putExtra(ACTION_HINT, hint);
i.putExtra(PROGRESS, proggress);
sendBroadcast(i);
}
课程中添加以下代码,以通知class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
//Log.e("Receiver", "Broadcast received: " + action);
if (action.equals(FileDownloader.ACTION_DOWNLOADMANEGAR)) {
String hint = intent.getStringExtra(FileDownloader.ACTION_HINT);
if (hint.equals(FileDownloader.ACTION_DOWNLOAD_ON_GOING)) {
//update your progrerssBar
progressBar.setProgress(intent.getIntExtra(FileDownloader.PROGRESS, 0));
// Log.e("Receiver", "progress: " + intent.getIntExtra(FileDownloader.PROGRESS, 0));
}
}
}
}
进度更改。下载开始时,启动private BroadcastReceiver myReceiver = new MyBroadcastReceiver();
private boolean isRegistered = false;
@override
onPause(){
supre.onPause();
if(isRegistered){
try {
// use only one between the two options bellow
// if u r using activity
unregisterReceiver(myReceiver);
// if u r using Fragment
getActivity().unregisterReceiver(myReceiver);
} catch (Exception e) {}
}
}
--------------------------------------------------------
@Override
public void onResume() {
super.onResume();
if(!isRegistered){
IntentFilter filter = new IntentFilter();
filter.addAction(FileDownloader.ACTION_DOWNLOADMANEGAR);
// use only one between the two options bellow
// if u r using activity
registerReceiver(myReceiver, filter);
// if u r using Fragment
getActivity().registerReceiver(myReceiver, filter);
isRegistered = true;
}
}
。
MainActiviy
这部分代码将像FM广播电台一样广播进度(int)。所有你需要做的就是,从我们的无线电接收来自无线电台的传输,就像从另一个课程中获取进展一样。收到:
<强> 1。在MainActivity中创建内部类:
MainActivity
按照以下步骤操作:
FileDownloader
此过程表示,如果您需要{{1}}并且下载正在进行中,那么{{1}}中的进度条会随着{{1}}中的更改下载进度而得到更新}。
如果您在执行上述程序时遇到任何问题,请告诉我。