我有我的第一堂课,它是一个加载画面。
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.但它不起作用。请任何人帮助我,只是第一次学习asynctasks。事实上,Java还是一个新手。我需要asynctask来加载5个声音,然后将活动从加载更改为主要活动。
答案 0 :(得分:3)
那是因为你正在打电话
if (switchscreensvalue == 1)
{
Intent myIntent = new Intent(Loading.this, main.class);
startActivityForResult(myIntent, 0);
}
在你的onCreate()中,你不能再次打电话给你了。
为什么不打电话
if (switchscreensvalue == 1)
{
Intent myIntent = new Intent(Loading.this, main.class);
startActivityForResult(myIntent, 0);
}
在设置变量之后的onPostExecute()中。
答案 1 :(得分:2)
这应该有用......
public class Loading extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadinglayout);
new Loadsounds().execute();
}
public void startMainActivity() {
Intent myIntent = new Intent(this, Main.class);
startActivityForResult(myIntent, 0);
}
private class Loadsounds extends AsyncTask<Void, Void, Boolean> {
boolean success = false;
@Override
protected Void doInBackground(Void... params) {
// Load sounds here and set success = true if successful
}
return success;
@Override
protected void onPostExecute(Boolean...result) {
if (result)
startMainActivity();
}
}
)
答案 2 :(得分:1)
你差不多......失去变量......
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadinglayout);
new Loadsounds().execute(switchscreensvalue);
}
// <Params, Progress, Result>
public class Loadsounds extends AsyncTask<Integer, Integer, Integer> {
@Override
protected Void doInBackground(Integer... params) {
int result = 0;
try {
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);
result = 1;
} catch(Exception e){
// some error handling if SoundManager.addSound throws exceptions?
}
return result; // positive integer on success
}
protected void onPostExecute(Integer result){
if (!isCancelled() && (result != null) && (result > 0)
{
// int someResult = 0;
Intent myIntent = new Intent(Loading.this, main.class);
// should the following line be startActivity?
// difference between startActivity and startActivityForResult
// is that the latter returns an integer value where your zero is
//startActivityForResult(myIntent, 0);
startActivityForResult(myIntent, someResult);
// Alternatively, just...
// startActivity(myIntent);
} else {
// Log error, inform user, then close application?
}
}
}
答案 3 :(得分:0)
我认为你想说的是它没有转换活动?我听到你对范围的看法,你确定这两个类都在同一个文件中吗?即一个类(LoadingTask)在另一个类(Loading类)中?
尝试将“Loading.this”切换为“getApplication()”
我有
public class Loading extends Activity {
protected void onCreate(...){
... (As in above answer)
}
// LoadTask exists inside the Loading activity class
private class LoadTask extends AsyncTask<Integer, Integer, Integer> {
protected Integer doInBackground(Integer... params) {
... (As in above answer)
}
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
try {
Intent intent = new Intent(Loading.this, Main.class);
// Alternatively
// Intent intent = new Intent(getApplication(), Main.class);
startActivity(intent);
} catch (ActivityNotFoundException e){
// Exception handling code
}
}
}
}