2014-05-18 20 views
-1

早先我使用以下代碼是工作:如何實現初始屏幕中類擴展片段

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(); 
    } 
} 

但是當我使用此代碼insted的上述代碼(類擴展活動/類擴展片段)

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> 
+0

請發佈您的logcat – Lal

+0

你不應該使用啓動畫面它不是一個好主意http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/ – Raghunandan

+0

哪裏有你添加你的片段和它被添加到什麼? 發佈一些更多的代碼和你得到的錯誤 – elmorabea

回答

0

你的片段類將有一個片段活動上課,對吧?或相關的Acivity課程是否正確?您需要在那裏啓動Splash Activity,一旦活動結束,您可以使用Fragment管理器加載新的Fragment,從此開始。

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>