java - 从初始屏幕导航到下一个屏幕时出错

标签 java android

我为我的 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);
}

我的 list 文件

<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)

LoadingTask 类

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
}

最佳答案

改变

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

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

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

关于java - 从初始屏幕导航到下一个屏幕时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13524800/

相关文章:

具有代理设置的 Android Studio Gradle Fabric "Unable to connect to the network"

android - 为什么不建议 fragment 之间直接通信?

java - 如何针对这种情况调整我的代码以在 Android 中进行 SAX XML 解析

java - 出现java.net.UnknownHostException,无法ping通任何网站,但可以正常浏览

java - 找不到参数的方法实现()[org.jetbrains.kotlin :kotlin-stdlib-jdk7:1. 3.50]

java - 编辑和更新相同的文本框

android - 协调启动其他应用程序的 Intent

android - 何时在 Android 中使用 RxJava,何时使用 Android Architectural Components 中的 LiveData?

JAVA删除API与数组字符串体

android - 在 view.getTag 获取空指针