这个异步任务正确吗?
我有我的第一堂课,它是一个加载屏幕。
public class Loading extends Activity {
public int switchscreensvalue = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadinglayout);
new Loadsounds().execute(switchscreensvalue);
if (switchscreensvalue == 1)
{
Intent myIntent = new Intent(Loading.this, main.class);
startActivityForResult(myIntent, 0);
}
}
}
然后我有我的 asynctask 类。
public class Loadsounds extends AsyncTask<Integer, Void, Void> {
@Override
protected Void doInBackground(Integer... params) {
SoundManager.addSound(0, R.raw.rubber);
SoundManager.addSound(1, R.raw.metal);
SoundManager.addSound(2, R.raw.ice);
SoundManager.addSound(3, R.raw.wind);
SoundManager.addSound(4, R.raw.fire);
return null;
}
protected void onPostExecute(Integer...params){
int switchscreensvalue = 1;
}
}
我希望它启动 asynctask,将 5 个声音加载到音板中,完成后,将 int“switchscreensvalue”更改为 1。然后,当“switchscreensvalue”= 1 时,加载屏幕应该更改为主屏幕。但它不起作用。请任何人帮助我,第一次学习异步任务。事实上,对于 Java 来说仍然相当陌生。我需要 asynctask 加载 5 个声音,然后将活动从加载更改为主要活动。
I have my first class, its a loading screen.
public class Loading extends Activity {
public int switchscreensvalue = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadinglayout);
new Loadsounds().execute(switchscreensvalue);
if (switchscreensvalue == 1)
{
Intent myIntent = new Intent(Loading.this, main.class);
startActivityForResult(myIntent, 0);
}
}
}
Then I have my asynctask class.
public class Loadsounds extends AsyncTask<Integer, Void, Void> {
@Override
protected Void doInBackground(Integer... params) {
SoundManager.addSound(0, R.raw.rubber);
SoundManager.addSound(1, R.raw.metal);
SoundManager.addSound(2, R.raw.ice);
SoundManager.addSound(3, R.raw.wind);
SoundManager.addSound(4, R.raw.fire);
return null;
}
protected void onPostExecute(Integer...params){
int switchscreensvalue = 1;
}
}
I want it to start the asynctask, which loads 5 sounds into a soundboard, and when its done, change the int "switchscreensvalue" to 1. Then, the loading screen is supposed to change to the main screen when "switchscreensvalue" = 1. It does not work though. Please can anyone help me, just learning asynctasks for the first time. Still fairly new to Java in fact. I need the asynctask to load 5 sounds and then change the activity from loading to main activity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那是因为你正在调用
onCreate() ,你不能保证它会再次被调用。
为什么不调用onPostExecute() 呢?
在设置变量后,
Thats because you are calling
in your onCreate() which you cant gaurantee will be called again.
Why dont you call
In your onPostExecute() after you set the variable as it should be.
这应该有效...
This should work...
你就快到了...失去变量...
Your almost there... Lose the variable...
我想你想说的是它没有切换活动?我听到你所说的范围,你确定这两个类都在同一个文件中吗?即,一个类(LoadingTask)位于另一个类(Loading 类)中?
尝试将“Loading.this”切换为“getApplication()”
我有
I think what your trying to say is that it's not switching activity? I hear what you say about scope, are you sure both classes are in the same file? ie, one class (LoadingTask) inside another (Loading class)?
try switching "Loading.this" for "getApplication()"
I have