扩展由android

时间:2017-08-18 12:07:58

标签: java android android-layout

我在Android studio中制作了一款游戏。它在模拟器和我自己的手机上运行良好。但是当我把它上传到Play商店时,我从一个活动的setContentView获得了Inflate Exception的HTC M8(在Android 6 Marshmallow上运行)的崩溃报告。我将提供Play商店的崩溃报告以及我的代码。

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.media.MediaPlayer;

import android.net.Uri;

import android.os.Bundle;
import android.support.annotation.*;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.leaderboard.Leaderboard;
import com.google.example.games.basegameutils.BaseGameActivity;
import com.google.example.games.basegameutils.BaseGameUtils;


public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener{
    MediaPlayer btnSound, gameStart;
    Button playa, instructo, high, exitar, googlesignout;
    private GoogleApiClient mGoogleApiClient;
    TextView hight;
    int highsco;
    public static final String COLORBANG = "MyPrefsFile";
    private static int RC_SIGN_IN = 9001;
    private static int RC_UNUSED = 5001;
    private boolean mResolvingConnectionFailure = false;
    private boolean mAutoStartSignInflow = true;
    private boolean mSignInClicked = false;
    boolean mExplicitSignOut = false;
    boolean mInSignInFlow = false;
    int n = 0;


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences prefs = getSharedPreferences(COLORBANG, MODE_PRIVATE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                .addApi(Drive.API).addScope(Drive.SCOPE_APPFOLDER)
                .build();
        btnSound = MediaPlayer.create(MainActivity.this, R.raw.buttonclick);
        gameStart = MediaPlayer.create(MainActivity.this, R.raw.splash);
        findViewById(R.id.sign_in_button).setOnClickListener(this);
        findViewById(R.id.full_version).setOnClickListener(this);
        gameStart.start();

        playa = (Button) findViewById(R.id.bPlay);
        googlesignout = (Button) findViewById(R.id.sign_out_button);
        instructo = (Button) findViewById(R.id.bInstructions);
        high = (Button) findViewById(R.id.bHighscore);
        highsco = prefs.getInt("chigh", 0);
        hight = (TextView) findViewById(R.id.tvHighscoreIndicator);
        exitar = (Button) findViewById(R.id.bExit);
        Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/Fipps-Regular.otf");
        playa.setTypeface(myCustomFont);
        hight.setTypeface(myCustomFont);
        instructo.setTypeface(myCustomFont);
        high.setTypeface(myCustomFont);
        exitar.setTypeface(myCustomFont);
        highTime();
        googlesignout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
                googlesignout.setVisibility(View.GONE);
                signOutclicked();
            }
        });
        playa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnSound.start();
                Intent startPlayin = new Intent(MainActivity.this, Layout.class);
                startActivity(startPlayin);
                finish();
            }
        });
        instructo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnSound.start();
                Intent instructi = new Intent(MainActivity.this, Instructions.class);
                startActivity(instructi);
            }
        });
        high.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnSound.start();
                if ( mGoogleApiClient.isConnected()) {
                    startActivityForResult(Games.Leaderboards.getAllLeaderboardsIntent(mGoogleApiClient),RC_UNUSED);
                }
                else{
                    Toast.makeText(MainActivity.this, "Please sign in to play games account", Toast.LENGTH_SHORT).show();
                }
            }
        });
        exitar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnSound.start();
                Toast.makeText(MainActivity.this, "Hasta La Vista Baby", Toast.LENGTH_SHORT).show();
                System.exit(0);

                finish();
            }
        });
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.

    }

    private void highTime() {
        if (highsco == 0) {
            hight.setVisibility(View.INVISIBLE);
        } else {
            hight.setText("YOUR HIGHSCORE : " + highsco + "");
            if (mGoogleApiClient.isConnected()) {
                Games.Leaderboards.submitScore(mGoogleApiClient, getString(R.string.leaderboard_color_score), highsco);
            }
            else{
                Toast.makeText(MainActivity.this,"Please sign in to ensure leaderboards updates", Toast.LENGTH_SHORT).show();
            }
        }

    }

    @Override
    public void onBackPressed() {
        if (n == 0) {
            Toast.makeText(MainActivity.this, "Press back again to exit", Toast.LENGTH_SHORT).show();
            n = n + 1;
        } else {
            Toast.makeText(MainActivity.this, "Hasta La Vista Baby", Toast.LENGTH_SHORT).show();
            System.exit(0);

            finish();
        }

    }

    private void gotoLink() {
        Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=cs.paidversion.arcade.game.colorbang");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mGoogleApiClient.disconnect();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!mInSignInFlow && !mExplicitSignOut) {
            // auto sign in
            mGoogleApiClient.connect();
        }

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        googlesignout.setVisibility(View.VISIBLE);
        if(highsco!=0){
            if (mGoogleApiClient.isConnected()) {
                Games.Leaderboards.submitScore(mGoogleApiClient, getString(R.string.leaderboard_color_score), highsco);
            }
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
// Attempt to reconnect
        mGoogleApiClient.connect();
    }
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent intent) {
        if (requestCode == RC_SIGN_IN) {
            mSignInClicked = false;
            mResolvingConnectionFailure = false;
            if (resultCode == RESULT_OK) {
                mGoogleApiClient.connect();
            } else {
                // Bring up an error dialog to alert the user that sign-in
                // failed. The R.string.signin_failure should reference an error
                // string in your strings.xml file that tells the user they
                // could not be signed in, such as "Unable to sign in."
                BaseGameUtils.showActivityResultError(this,
                        requestCode, resultCode, R.string.signin_failure);
            }
        }
    }
    private void signInClicked() {
        mSignInClicked = true;
        mGoogleApiClient.connect();
    }
    private void signOutclicked() {
        mSignInClicked = false;
        mExplicitSignOut = true;
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            Games.signOut(mGoogleApiClient);
            mGoogleApiClient.disconnect();
        }

    }
    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        if (mResolvingConnectionFailure) {
            // already resolving
            return;
        }

        // if the sign-in button was clicked or if auto sign-in is enabled,
        // launch the sign-in flow
        if (mSignInClicked || mAutoStartSignInflow) {
            mAutoStartSignInflow = false;
            mSignInClicked = false;
            mResolvingConnectionFailure = true;

            // Attempt to resolve the connection failure using BaseGameUtils.
            // The R.string.signin_other_error value should reference a generic
            // error string in your strings.xml file, such as "There was
            // an issue with sign-in, please try again later."
            if (!BaseGameUtils.resolveConnectionFailure(this,
                    mGoogleApiClient, connectionResult,
                    RC_SIGN_IN, R.string.signin_other_error)) {
                mResolvingConnectionFailure = false;
            }
        }

        findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
        googlesignout.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sign_in_button) {
             signInClicked();
        }
        else if (v.getId() == R.id.full_version) {
            gotoLink();
        }
    }




}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="cs.puzzle.game.paid.colorbangv2.MainActivity"
    android:background="@drawable/mainback">


    <Button
        android:text="PLAY"
        android:layout_width="260dp"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/blackbutton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="240dp"
        android:id="@+id/bPlay" />
    <Button
        android:text="INSTRUCTIONS"
        android:layout_width="260dp"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_height="50dp"
        android:layout_below="@id/bPlay"
        android:layout_marginTop="20dp"
        android:background="@drawable/blackbutton"
        android:layout_centerHorizontal="true"
        android:id="@+id/bInstructions" />
    <Button
        android:text="LEADERBOARDS"
        android:layout_width="260dp"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_height="50dp"
        android:layout_below="@id/bInstructions"
        android:layout_marginTop="20dp"
        android:background="@drawable/blackbutton"
        android:layout_centerHorizontal="true"
        android:id="@+id/bHighscore" />
    <Button
        android:text="EXIT"
        android:layout_width="260dp"

        android:gravity="center"
        android:textSize="20sp"
        android:layout_height="50dp"
        android:layout_below="@id/bHighscore"
        android:layout_marginTop="20dp"
        android:background="@drawable/blackbutton"
        android:layout_centerHorizontal="true"
        android:id="@+id/bExit" />

    <TextView
        android:text="YOUR HIGHSCORE : XX"
        android:textSize="15sp"
        android:textColor="@color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="75dp"
        android:id="@+id/tvHighscoreIndicator" />

    <Button
        android:text="Sign out"
        android:gravity="center"

        android:textSize="10sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:background="@drawable/blackbutton"
        android:drawableLeft="@drawable/ic_action_name"

        android:visibility="gone"
        android:id="@+id/sign_out_button" />
    <com.google.android.gms.common.SignInButton
        android:id="@+id/sign_in_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:text="Get Full version"
        android:gravity="center"
        android:padding="10dp"
        android:textSize="12sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/blackbutton"
        android:id="@+id/full_version" />
</RelativeLayout>

来自Play商店的崩溃报告

java.lang.RuntimeException: 

  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2423)

  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2483)

  at android.app.ActivityThread.access$900 (ActivityThread.java:153)

  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1349)

  at android.os.Handler.dispatchMessage (Handler.java:102)

  at android.os.Looper.loop (Looper.java:148)

  at android.app.ActivityThread.main (ActivityThread.java:5438)

  at java.lang.reflect.Method.invoke (Native Method)

  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:738)

  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:628)
Caused by: android.view.InflateException: 

  at android.view.LayoutInflater.inflate (LayoutInflater.java:543)

  at android.view.LayoutInflater.inflate (LayoutInflater.java:427)

  at android.view.LayoutInflater.inflate (LayoutInflater.java:374)

  at com.android.internal.policy.PhoneWindow.setContentView (PhoneWindow.java:393)

  at android.app.Activity.setContentView (Activity.java:2183)

  at cs.puzzle.game.paid.colorbangv2.MainActivity.onCreate (MainActivity.java:62)

  at android.app.Activity.performCreate (Activity.java:6303)

  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1108)

  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2376)

0 个答案:

没有答案