如何在类中实现启动画面扩展Fragment

时间:2014-05-18 07:43:34

标签: java android android-fragments

之前我正在使用下面有效的代码:

package com.usd.quiztest;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;


public class Logo extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logo_screen);

        Thread background = new Thread() {
            public void run() {

                try {
                    // Thread will sleep for 5 seconds
                    sleep(2000);

                    // After 5 seconds redirect to another intent
                    Intent i=new Intent(getBaseContext(),First.class);
                    startActivity(i);

                    //Remove activity
                    finish();

                } catch (Exception e) {

                }
            }
        };

        // start thread
        background.start();
    }
}

但是当我使用上面这段代码的代码时(类扩展activity / class extends Fragment)

    package com.usd.quiztest;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Logo extends Fragment{ 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.logo_screen, null);
    }
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        Thread background = new Thread() {
            public void run() {

                try {
                    // Thread will sleep for 5 seconds
                    sleep(2000);

                    // After 5 seconds redirect to another intent
                    Intent i=new Intent(getBaseContext(),First.class);
                    startActivity(i);

                    //Remove activity
                    finish();

                } catch (Exception e) {

                }
            }
        };

        // start thread
        background.start();

    }
}

我的应用程序结束时幸运的是停止错误

帮助我在课程中实现启动画面扩展片段

错误:

Description Resource    Path    Location    Type
Intent cannot be resolved to a type Logo.java   /QuizTest/src/com/usd/quiztest  line 25 Java Problem

Description Resource    Path    Location    Type
Intent cannot be resolved to a type Logo.java   /QuizTest/src/com/usd/quiztest  line 25 Java Problem

Description Resource    Path    Location    Type
The method finish() is undefined for the type new Thread(){}    Logo.java   /QuizTest/src/com/usd/quiztest  line 29 Java Problem

Description Resource    Path    Location    Type
The method getBaseContext() is undefined for the type new Thread(){}    Logo.java   /QuizTest/src/com/usd/quiztest  line 25 Java Problem

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.usd.quiztest"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <supports-screens>
        android:resizeable="true"
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"
    </supports-screens>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.usd.quiztest.Logo"
            android:label="@string/app_name"            
            android:theme="@android:style/Theme.Black.NoTitleBar" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />               
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <activity
            android:name="com.usd.quiztest.First"            
            android:label="@string/app_name"
            android:clearTaskOnLaunch="true" >            
        </activity>
        <activity
            android:name="com.usd.quiztest.Q1"
            android:label="@string/app_name" >
        </activity>        
         <activity
            android:name="com.usd.quiztest.Q2"
            android:label="@string/app_name" >
        </activity>
         <activity
            android:name="com.usd.quiztest.Q3"
            android:label="@string/app_name" >
        </activity>
         <activity
            android:name="com.usd.quiztest.Q4"
            android:label="@string/app_name" >
        </activity>
         <activity
            android:name="com.usd.quiztest.Q5"
            android:label="@string/app_name" >
        </activity>
         <activity
            android:name="com.usd.quiztest.FinalPage"
            android:label="@string/app_name" >
        </activity>
         <activity
            android:name="com.usd.quiztest.Score"
            android:label="@string/app_name" >
        </activity>

    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

您可以在SplashScreenActivity中调用run()方法。

我的项目正在使用appcompat_v7库。

延迟2000毫秒后,将执行run()方法。

SplashScreenActivity.java

package com.usd.quiztest;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;

public class SplashScreenActivity extends ActionBarActivity implements Runnable {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        if (savedInstanceState == null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.container, new SplashScreenFragment());
            fragmentTransaction.commit();
        }

        Handler handler = new Handler();
        handler.postDelayed(this, 2000);
    }

    public void run() {
        Intent intent = new Intent(this, FirstActivity.class);
        startActivity(intent);
        finish();
    }
}

activity_splash_screen.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.usd.quiztest.SplashScreenActivity"
    tools:ignore="MergeRootFrame" />

SplashScreenFragment.java

package com.usd.quiztest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SplashScreenFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_splash_screen, container, false);
    }
}

fragment_splash_screen.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.usd.quiztest.SplashScreenActivity$SplashScreenFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/splash_screen" />

</RelativeLayout>

答案 1 :(得分:0)

你的Fragment类会有一个Fragment Activity类,对吗?或相关的Acivity类吧?你需要在那里开始你的Splash Activity,一旦活动结束,你可以从那里使用Fragment manager加载新的Fragment。