2

我为我的 android 项目创建了启动画面,如果我运行它,启动画面会出现一段时间并显示强制关闭消息,我应该怎么做才能导航到下一页?有什么建议么?

public class LoadingScreen extends Activity implements LoadingTaskFinishedListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Show the splash screen
    setContentView(R.layout.activity_loading_screen);
    // Find the progress bar
    ProgressBar progressBar = (ProgressBar) findViewById(R.id.Progressbar);
    // Start your loading
    new LoadingTask(progressBar, null).execute("www.google.co.uk"); // Pass in whatever you need a url is just an example we don't use it in this tutorial

}

// This is the callback for when your async task has finished
public void onTaskFinished() {
    completeSplash();
}

private void completeSplash(){
    startApp();
    finish(); // Don't forget to finish this Splash Activity so the user can't return to it!
}

private void startApp() {
    Intent intent = new Intent(LoadingScreen.this, Rebuix.class);
    startActivity(intent);
}

}

我的清单文件

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:icon="@drawable/rebuix"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".LoadingScreen"
        android:label="@string/title_activity_loading_screen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Rebuix"
        android:label="@string/title_activity_rebuix" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.rebuix.com.Rebuix" />
    </activity>
    <activity
        android:name=".Login"
        android:label="@string/title_activity_login" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.rebuix.com.Rebuix" />
    </activity>

日志猫

11-23 13:13:03.798: I/Tutorial(459): Starting task with url: www.google.co.uk
11-23 13:13:14.156: D/AndroidRuntime(459): Shutting down VM
11-23 13:13:14.156: W/dalvikvm(459): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-23 13:13:14.166: E/AndroidRuntime(459): FATAL EXCEPTION: main
11-23 13:13:14.166: E/AndroidRuntime(459): java.lang.NullPointerException
11-23 13:13:14.166: E/AndroidRuntime(459):  at com.rebuix.com.LoadingTask.onPostExecute(LoadingTask.java:68)
11-23 13:13:14.166: E/AndroidRuntime(459):  at com.rebuix.com.LoadingTask.onPostExecute(LoadingTask.java:1)
11-23 13:13:14.166: E/AndroidRuntime(459):  at android.os.AsyncTask.finish(AsyncTask.java:417)
11-23 13:13:14.166: E/AndroidRuntime(459):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-23 13:13:14.166: E/AndroidRuntime(459):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-23 13:13:14.166: E/AndroidRuntime(459):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 13:13:14.166: E/AndroidRuntime(459):  at android.os.Looper.loop(Looper.java:123)
11-23 13:13:14.166: E/AndroidRuntime(459):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-23 13:13:14.166: E/AndroidRuntime(459):  at java.lang.reflect.Method.invokeNative(Native Method)
11-23 13:13:14.166: E/AndroidRuntime(459):  at java.lang.reflect.Method.invoke(Method.java:521)
11-23 13:13:14.166: E/AndroidRuntime(459):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-23 13:13:14.166: E/AndroidRuntime(459):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-23 13:13:14.166: E/AndroidRuntime(459):  at dalvik.system.NativeStart.main(Native Method)

加载任务类

public class LoadingTask extends AsyncTask<String, Integer, Integer> {

public interface LoadingTaskFinishedListener {
    void onTaskFinished(); // If you want to pass something back to the listener add a param to this method
}

// This is the progress bar you want to update while the task is in progress
private final ProgressBar progressBar;
// This is the listener that will be told when this task is finished
private final LoadingTaskFinishedListener finishedListener;

/**
 * A Loading task that will load some resources that are necessary for the app to start
 * @param progressBar - the progress bar you want to update while the task is in progress
 * @param finishedListener - the listener that will be told when this task is finished
 */
public LoadingTask(ProgressBar progressBar, LoadingTaskFinishedListener finishedListener) {
    this.progressBar = progressBar;
    this.finishedListener = finishedListener;
}

@Override
protected Integer doInBackground(String... params) {
    Log.i("Tutorial", "Starting task with url: "+params[0]);
    if(resourcesDontAlreadyExist()){
        downloadResources();
    }
    // Perhaps you want to return something to your post execute
    return 1234;
}

private boolean resourcesDontAlreadyExist() {
    // Here you would query your app's internal state to see if this download had been performed before
    // Perhaps once checked save this in a shared preference for speed of access next time
    return true; // returning true so we show the splash every time
}


private void downloadResources() {
    // We are just imitating some process thats takes a bit of time (loading of resources / downloading)
    int count = 10;
    for (int i = 0; i < count; i++) {

        // Update the progress bar after every step
        int progress = (int) ((i / (float) count) * 100);
        publishProgress(progress);

        // Do some long loading things
        try { Thread.sleep(1000); } catch (InterruptedException ignore) {}
    }
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    progressBar.setProgress(values[0]); // This is ran on the UI thread so it is ok to update our progress bar ( a UI view ) here
}

@Override
protected void onPostExecute(Integer result) {
    super.onPostExecute(result);
    finishedListener.onTaskFinished(); // Tell whoever was listening we have finished
}

}

4

1 回答 1

0

改变

new LoadingTask(progressBar, null).execute("www.google.co.uk");

new LoadingTask(progressBar, this).execute("www.google.co.uk");

我认为第二个参数应该是 LoadingTaskFinishedListener。

于 2012-11-23T08:01:16.743 回答