启动画面崩溃

时间:2018-08-21 20:52:29

标签: java android android-studio crash splash-screen

我是android studio的新手,我正在开发我的第一个应用程序。

一切正常,直到我实现了启动画面。确实,当我运行该应用程序时,splashcreen起作用了,但是随后崩溃了。

问题是,启动画面不是我创建的第一个活动(主要活动)。我已经创建了我的应用程序,完成后,我添加了一个启动画面。

实际上,当我禁用启动画面(活动)并直接从上一个“启动时出现的活动”(即“菜单”)开始时,它可以正常工作。

一件奇怪的事情:启用启动画面后,它仅在某些设备上崩溃。它可以与我的Xiaomi Redmi Note 5和Galaxy S4(或nexus 5X模拟器)完美配合,但不能与Galaxy S3和S6完美配合。

我试图用一个简单的setonclicklistener按钮替换计时器。但是同样,当它必须加载菜单时,它会崩溃。

我没有在论坛上找到类似的问题,所以我在这里!

清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="false"
    android:theme="@style/AppTheme">

    <activity android:name=".Menu"/>
    <activity android:name=".restaurant" />
    <activity android:name=".courses" />
    <activity android:name=".toilettes" />
    <activity android:name=".distributeur" />
    <activity android:name=".essence" />
    <activity android:name=".hopital" />
    <activity android:name=".Splashscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name=".MainActivity"/>


    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />
</application>

这是Splashscreen -java

package fr.libertyg.messervices;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Handler;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Timer;

public class Splashscreen extends AppCompatActivity {

private ImageView daccord;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_splashscreen);

    final TextView random = (TextView) findViewById(R.id.random);
    final String[] mesphrases = {"S1", "S2",
            "S3", "S4", "S5","S6"};
    int rando = (int) (Math.random() * 6);
    random.setText(mesphrases[rando]);

    this.daccord = findViewById(R.id.daccord);
    daccord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent otheractivity = new Intent(Splashscreen.this, Menu.class);
            startActivity(otheractivity);
            finish();


        }
    });






}

Splashscreen xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Splashscreen"
android:weightSum="10"
android:orientation="vertical"
android:background="@drawable/splashscreenfond">

<RelativeLayout
    android:layout_weight="1.5"
    android:layout_width="match_parent"
    android:layout_height="0dp">

    <TextView
        android:id="@+id/spashscreenbienvenue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/splashscreenbienvenue"
        android:textSize="40sp" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="249dp"
        android:layout_height="217dp"
        android:layout_centerInParent="true"
        android:src="@drawable/icon" />

    <TextView
        android:id="@+id/nom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/app_name"
        android:textSize="30sp" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.5">

    <TextView
        android:id="@+id/bonasavoir"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="@string/bonasavoir"
        android:textSize="17sp" />


    <TextView
        android:id="@+id/donnees"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/random"
        android:layout_centerHorizontal="true"
        android:text="@string/activezgps"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/random"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="13dp"
        android:text="Un être humain a besoin d'environ 2000 kcal/jour"
        android:textSize="14sp" />


</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2">


    <TextView
        android:id="@+id/version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/version" />

    <ImageView
        android:id="@+id/daccord"
        android:layout_width="124dp"
        android:layout_height="105dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/daccord" />
</RelativeLayout>




</LinearLayout>

和菜单-java

package fr.libertyg.messervices;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

public class Menu extends AppCompatActivity {

private ImageView distributeur;
private ImageView restaurant;
private ImageView courses;
private ImageView toilettes;
private ImageView essence;
private ImageView hopital;
AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_menu);

    this.distributeur = findViewById(R.id.distributeur);

    distributeur.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent otherActivity = new Intent(getApplicationContext(), distributeur.class);
            startActivity(otherActivity);

        }
    });

    this.restaurant = findViewById(R.id.restaurant);

    restaurant.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent otherActivity = new Intent(getApplicationContext(), restaurant.class);
            startActivity(otherActivity);

        }
    });

    this.courses = findViewById(R.id.courses);

    courses.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent otherActivity = new Intent(getApplicationContext(), courses.class);
            startActivity(otherActivity);

        }
    });

    this.toilettes = findViewById(R.id.toilettes);

    toilettes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent otherActivity = new Intent(getApplicationContext(), toilettes.class);
            startActivity(otherActivity);

        }
    });

    this.essence = findViewById(R.id.essence);

    essence.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent otherActivity = new Intent(getApplicationContext(), essence.class);
            startActivity(otherActivity);

        }
    });

    this.hopital = findViewById(R.id.hopital);

    hopital.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent otherActivity = new Intent(getApplicationContext(), hopital.class);
            startActivity(otherActivity);

        }
    });

    MobileAds.initialize(this, "ca-app-pub-6520930039267956~4038950000");
    mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);









}

}

还有菜单xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/fond"
android:orientation="vertical"
android:weightSum="10">

<RelativeLayout
    android:layout_weight="1.5"
    android:layout_width="match_parent"
    android:layout_height="0dp">

    <TextView
        android:id="@+id/bienvenue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/quoi"
        android:textColor="@color/blanc"
        android:textSize="30sp"/>
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="7.6">


    <ImageView
        android:id="@+id/distributeur"
        android:layout_width="140dp"
        android:layout_height="130dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginStart="37dp"
        android:layout_marginTop="29dp"
        android:src="@drawable/distributeur" />

    <ImageView
        android:id="@+id/restaurant"
        android:layout_width="140dp"
        android:layout_height="130dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginEnd="37dp"
        android:layout_marginTop="29dp"
        android:src="@drawable/restaurant" />

    <ImageView
        android:id="@+id/courses"
        top="@+id/distributeur"
        android:layout_width="140dp"
        android:layout_height="130dp"
        android:layout_alignStart="@+id/distributeur"
        android:layout_centerVertical="true"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/courses" />

    <ImageView
        android:id="@+id/toilettes"
        android:layout_width="140dp"
        android:layout_height="130dp"
        android:layout_alignStart="@+id/restaurant"
        android:layout_alignTop="@+id/courses"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/toilettes" />

    <ImageView
        android:id="@+id/essence"
        android:layout_width="140dp"
        android:layout_height="130dp"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/distributeur"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="28dp"
        android:src="@drawable/essence" />

    <ImageView
        android:id="@+id/hopital"
        android:layout_width="140dp"
        android:layout_height="130dp"
        android:layout_alignStart="@+id/restaurant"
        android:layout_alignTop="@+id/essence"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/hopital" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9">

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-6520930039267956/3068657000" />

</RelativeLayout>

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

在您的应用gradle中,确保库使用相同的版本,例如:

implementation 'com.android.support:design:28.0.0-alpha1'
implementation 'com.android.support:cardview-v7:28.0.0-alpha1'
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
implementation 'com.android.support:gridlayout-v7:28.0.0-alpha1'

您可能不需要全部。
进行更改,然后单击“立即同步”

答案 1 :(得分:0)

解决了:

我必须将此添加到清单中

android:largeHeap="true"

效果很好。