我开始认为要使ProgressDialog工作,AsyncTask必须是Activity类中的内部类.真正?[编辑很久以后......答案是假的,我不确定这是一个错误还是什么.我使用的是Android 1.5.我打算读一读服务.]
我有一个活动,使用数据库来操纵信息.如果填充数据库一切都很好.如果没有填充,那么我需要从网站下载信息,填充数据库,然后访问填充的数据库以完成onCreate中的视图.
问题是没有办法确定AsyncTask线程何时完成填充数据库,我得到以下强制关闭错误消息:抱歉!应用程序意外停止.我点击Force Close按钮,后台AsyncTask线程继续工作,数据库被填充,一切正常.
我需要摆脱该错误消息,并需要一些帮助来解决这个问题.这是一些伪代码:
public class ViewStuff extends Activity
{
onCreate
{
if(database is populated)
do_stuff
else
{
FillDB task = null;
if(task == null || task.getStatus().equals(AsyncTask.Status.FINISHED))
{
task = new FillDB(context);
task.execute(null);
}
}
continue with onCreate using information from database to properly display
} // end onCreate
} // end class
Run Code Online (Sandbox Code Playgroud)
在单独的文件中:
public class FillDB extends AsyncTask<Void, Void, Void>
{
private Context context;
public FillDB (Context c) //pass the context in the constructor
{
context = …Run Code Online (Sandbox Code Playgroud) 我正在制作照片轮播。目的是在一行上显示 x 张图片,其中 x 取决于视口大小。我所说的视口是指浏览器窗口内没有滚动条的区域。照片的大小调整为宽度 200 如果视口宽度为 2000 我可以获得 10 张照片 (200*10=2000) (我保持算术简单)
我的屏幕分辨率是 3840x2160。我扩大了浏览器以占据整个屏幕。当我使用: $(window).width() 时,我得到 1707,而 screen.width 则得到 1707。绝对不是 3840。
当我调整浏览器大小,使其占据大约一半的屏幕时,我得到: $(window).width() 为 929,screen.width 为 1706。
对于我使用 Edge、IE11、FF 和 Chrome 46 的浏览器,我遇到了大致相同的问题。
我的桌面显示器也是 4k,据我所知,它由两个 1920x1080 的面板组成,总共 3840x2160。如果这是真的,在我的笔记本电脑 4k 显示器上,如果我占据整个屏幕,我应该使用 screen.width 获得 1920 的宽度,但我没有。
我需要考虑到大多数浏览此网站的人都没有 4k 显示器。
关于如何获取 4k 显示器上的屏幕宽度有什么想法吗?
我搜索了Stackoverflow,查看了Android Developer中的示例,以及一些书籍,我只是没有得到它.
我正在尝试显示一个中断程序流的AlertDialog Box.我见过的大多数例子都没有对话框后的代码,我需要程序停止执行,直到按下AlertDialog按钮.好像AlertDialog是从另一个线程创建的,我不知道如何检索该线程.
程序逻辑:如果解析不好,程序将强制关闭.我想让用户知道重新启动程序,一切都会正常工作.(我正在删除并重新创建表,并在程序启动时重新填充它们)
这是一些代码:
if(database populated)
{
....code.....
if(parseok.contentEquals("Error"))
{
doForceClose();
}
displayDate = "Last: " + parseok; //I don't want the program to continue to here.
//Rest of code in the method. If I continue the program will Force Close
}
else
do something else
Run Code Online (Sandbox Code Playgroud)
这是AlertDialog方法:
private void doForceClose()
{
String themessage = "Because some of the parameters have changed in the yada yada";
AlertDialog.Builder ad = new AlertDialog.Builder (this);
ad.setTitle("Internal Error");
ad.setMessage(themessage);
ad.setPositiveButton("Sorry", new OnClickListener()
{ …Run Code Online (Sandbox Code Playgroud)