Java 如何在类扩展片段中实现初始屏幕

Java 如何在类扩展片段中实现初始屏幕,java,android,android-fragments,Java,Android,Android Fragments,之前,我使用的代码如下: 但是当我使用此代码而不是上面的代码(类扩展活动/类扩展片段) 我的应用程序最后出现停止错误 帮助我在类扩展片段中实现启动屏幕 package com.usd.quiztest; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.

之前,我使用的代码如下:

但是当我使用此代码而不是上面的代码(类扩展活动/类扩展片段)

我的应用程序最后出现停止错误

帮助我在类扩展片段中实现启动屏幕

    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>

android:resizeable=“true”
android:smallScreens=“true”
android:normalScreens=“true”
android:largeScreens=“true”
android:xlargeScreens=“true”
android:anyDensity=“true”

您的片段类将有一个片段活动类,对吗?或者相关的职业,对吗?您需要在那里启动Splash活动,活动完成后,您可以使用片段管理器加载新片段。

您可以在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" />
<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>
fragment_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" />
<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>


请发布您的日志。您不应该使用闪屏。这不是一个好主意,您将在哪里添加您的片段,它将添加到什么?发布更多的代码和错误