在 AsyncTask doInBackground 中执行函数时出错
我正在尝试从 URL 加载一些图像。
我有一个私人空间来完成所有的工作。它有效...但我有一个问题,我想在加载图像时显示对话框进度...
我正在尝试使用 AsyncTask,但在执行 doInBackground 时遇到问题!
这是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new EjecutarTarea().execute();
}
private void Principal() {
//Code to download images from URL
}
public class EjecutarTarea extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
dialog = new ProgressDialog(StadioNenaActivity.this);
dialog.setMessage("Cargando..");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
protected Void doInBackground(Void... Unsed) {
Principal(); //i have an error here
return null;
}
protected void onProgressUpdate(Void... unused) {
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
感谢您的帮助!
这是 de log 上的错误...它在我的代码 PRINCIPAL() 的某些部分返回 null ....
我不知道为什么,当我将 PRINCIPAL 放入 Oncreate 中时,它工作得很好!
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): FATAL EXCEPTION: AsyncTask #1
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): java.lang.RuntimeException: An error occured while executing doInBackground()
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at android.os.AsyncTask$3.done (AsyncTask.java:200)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.lang.Thread.run(Thread.java:1096)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): Caused by: java.lang.NullPointerException
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at org.json.JSONTokener.nextValue(JSONTokener.java:90)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at org.json.JSONArray.<init>(JSONArray.java:87)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at org.json.JSONArray.<init>(JSONArray.java:103)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at Stadio.bacha.stadionena.StadioNenaActivity.Principal(StadioNenaActivity.java:121)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at Stadio.bacha.stadionena.StadioNenaActivity$EjecutarTarea.doInBackground(StadioNenaActivity.java:262)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at Stadio.bacha.stadionena.StadioNenaActivity$EjecutarTarea.doInBackground(StadioNenaActivity.java:1)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): ... 4 more
当我这样做时:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Principal();
}
它可以工作,但它不显示对话框进度......
Im trying to load some images from a URL.
I have a private void that makes all the work. It works... but i have a problem, i want to show a dialog progress while my images are loading...
I am trying with an AsyncTask but i have a problem when executing the doInBackground!
This is my code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new EjecutarTarea().execute();
}
private void Principal() {
//Code to download images from URL
}
public class EjecutarTarea extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
dialog = new ProgressDialog(StadioNenaActivity.this);
dialog.setMessage("Cargando..");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
protected Void doInBackground(Void... Unsed) {
Principal(); //i have an error here
return null;
}
protected void onProgressUpdate(Void... unused) {
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
Thanks for the help !.
This is the error on de log... it return null in some part of my code PRINCIPAL() ....
i dont know why, when i put PRINCIPAL in Oncreate, it works great!.
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): FATAL EXCEPTION: AsyncTask #1
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): java.lang.RuntimeException: An error occured while executing doInBackground()
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at android.os.AsyncTask$3.done (AsyncTask.java:200)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.lang.Thread.run(Thread.java:1096)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): Caused by: java.lang.NullPointerException
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at org.json.JSONTokener.nextValue(JSONTokener.java:90)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at org.json.JSONArray.<init>(JSONArray.java:87)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at org.json.JSONArray.<init>(JSONArray.java:103)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at Stadio.bacha.stadionena.StadioNenaActivity.Principal(StadioNenaActivity.java:121)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at Stadio.bacha.stadionena.StadioNenaActivity$EjecutarTarea.doInBackground(StadioNenaActivity.java:262)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at Stadio.bacha.stadionena.StadioNenaActivity$EjecutarTarea.doInBackground(StadioNenaActivity.java:1)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-10 17:56:01.988: ERROR/AndroidRuntime(10907): ... 4 more
When i go like this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Principal();
}
it works, but it does not show dialog progress...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是调用
doInBackground()
时出现NullPointerException。确保在调用 AsyncTask.execute() 之前所有数据可用或提供某种阻塞机制。The problem is that there is a NullPointerException when
doInBackground()
is called. Ensure that all data is available before callingAsyncTask.execute()
or provide some kind of blocking mechanism.确保您的网址正确。
我收到此错误是因为我使用了 192.168.1.173/webroot 路径...
我的意思是没有我们的 http:// 部分作为 url。
我尝试使用后
效果很好。
make sure your url is correct.
I got this error because i used 192.168.1.173/webroot path...
I mean withour http:// part as url.
After I tried with
it works fine.