Android Splash屏幕无法加载其他活动

时间:2015-04-18 11:37:26

标签: android

以下是我的Android启动画面活动的代码。

当它尝试加载另一个活动时,应用程序在Android 4.0和4.1上崩溃。

我不知道造成这种情况的原因是什么,因为当它崩溃时,它不会记录任何错误。

有没有人遇到过像这样的事情?

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.bitbucket.infovillafoundation.denko.R;
import org.bitbucket.infovillafoundation.denko.component.DaggerDenkoStationComponent;
import org.bitbucket.infovillafoundation.denko.component.DenkoStationComponent;
import org.bitbucket.infovillafoundation.denko.models.DenkoModel;
import org.bitbucket.infovillafoundation.denko.module.DenkoStationModule;
import org.bitbucket.infovillafoundation.denko.service.DenkoStationService;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;


public class SplashScreenActivity extends Activity {

    @InjectView(R.id.imgLogo)
    ImageView logoImage;

    @InjectView(R.id.welcomeText)
    TextView welcomeText;

    private Handler splashHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Runnable r = new Runnable() {
            @Override
            public void run() {
                //********************* application is crashing here
                Intent mainIntent = new Intent(SplashScreenActivity.this, LanguageOptionActivity.class);
                startActivity(mainIntent);
                finish();
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        };

        setContentView(R.layout.activity_splash);

        ButterKnife.inject(this);

        DenkoStationComponent component = DaggerDenkoStationComponent.builder().denkoStationModule(new DenkoStationModule()).build();
        final DenkoStationService denkoStationService = component.provideDenkoStationService();

        Callback<DenkoModel> callback = new Callback<DenkoModel>() {
            @Override
            public void success(DenkoModel denkoModel, Response response) {
                toast(R.string.server_connection_successful);
                denkoStationService.updateDatabaseWithDenkoModel(denkoModel);
            }

            @Override
            public void failure(RetrofitError error) {
                toast(R.string.server_connection_failed);
            }

            public void toast(int textId) {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_layout,
                        (ViewGroup) findViewById(R.id.toast_layout_root));

                TextView text = (TextView) layout.findViewById(R.id.toast_text);
                text.setText(getResources().getString(textId));

                Toast toast = new Toast(getApplicationContext());
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
        };

        denkoStationService.fetchDenkoModel(denkoStationService.fetchDenkoLastDataState(), callback);

        if (isNetworkAvailable())
            splashHandler.postDelayed(r, 3000);
        else {
            toast(R.string.no_internet_connection);
            splashHandler.postDelayed(r, 3000);
        }

        //********************* application is crashing here
        Intent mainIntent = new Intent(this, LanguageOptionActivity.class);
        startActivity(mainIntent);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }

    private void toast(int textId) {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout,
                (ViewGroup) findViewById(R.id.toast_layout_root));

        TextView text = (TextView) layout.findViewById(R.id.toast_text);
        text.setText(getResources().getString(textId));

        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }
}

修改:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.bitbucket.infovillafoundation.denko">

    <uses-sdk android:minSdkVersion="14" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <permission
        android:name="org.bitbucket.infovillafoundation.denko.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-permission android:name="org.bitbucket.infovillafoundation.denko.MAPS_RECEIVE" />

    <application
        android:name=".application.DenkoApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activity.SplashScreenActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activity.MainActivity"
            android:label="@string/title_activity_home"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar" />
        <activity
            android:name=".activity.LanguageOptionActivity"
            android:label="@string/title_activity_language_option"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar" />

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAubZsgoC4Z64qcOSpfK4grjrK5zrTEWxk" />
    </application>

</manifest>

我认为,由于Android版本不会出现崩溃,因此问题可能是由于OpenGL版本不兼容造成的。

3 个答案:

答案 0 :(得分:0)

我认为您无法在AndroidManifest.xml.plz中添加启动画面检查

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


   public class Splash extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
          setContentView(R.layout.splash);

    Thread logoTimer = new Thread() {
        public void run() {
            try {
                int logoTimer = 0;
                while (logoTimer < 500) {
                    sleep(100);
                    logoTimer = logoTimer + 100;
                }

                Intent newIntent = new Intent(Splash.this,      MainActivity.class);
                startActivity(newIntent);
            } catch(InterruptedException e) {
                e.printStackTrace();
            } finally {
                finish();
            }
        }
    };
    logoTimer.start();
}

}

Android Manifest.xml中的

        <activity
        android:name=".Splash"
        android:label="@string/app_name" >
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>

答案 1 :(得分:0)

您可以将以下活动与一些简单内容一起使用。注入布局(见下文)并在布局文件中注册一个onclick-listener(方法&#34; forward&#34;)。在此方法中,您可以调用下一个活动:

public class SplashScreen extends Activity{

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

    public void forward(View v){
        startActivity(new Intent(this, MainActivity.class), 0);
    }
}

splashscreen.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    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=".MainActivity" >
    <ImageView
        android:id="@+id/splashscreen"
        android:onClick="forward"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

添加AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
    <activity
        android:name=".SplashScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity" />
</application>

课程&#34; MainActivity&#34;代表您的主要活动和课程&#34; SplashScreen&#34;代表一个简单的启动画面。

我希望这会对你有所帮助:)。

答案 2 :(得分:0)

我看到的问题是,在调用activity标记时,你仍然在name属性中使用.activity 即你使用android:name=".activity.MainActivity" 对于启动画面活动和主要活动,我们都不是android:name=".MainActivity"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.bitbucket.infovillafoundation.denko">

    <uses-sdk android:minSdkVersion="14" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <permission
        android:name="org.bitbucket.infovillafoundation.denko.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-permission android:name="org.bitbucket.infovillafoundation.denko.MAPS_RECEIVE" />

    <application
        android:name=".application.DenkoApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".SplashScreenActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_home"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar" />
        <activity
            android:name=".activity.LanguageOptionActivity"
            android:label="@string/title_activity_language_option"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar" />

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAubZsgoC4Z64qcOSpfK4grjrK5zrTEWxk" />
    </application>

</manifest>