简体   繁体   中英

Google play game services sign in/out buttons

Im trying to implement google play game services sign in/out buttons as its suggested here: https://developers.google.com/games/services/training/signin

But whenever

findViewById(R.id.sign_in_button).setVisibility(View.GONE); 

or

findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE); 

gets called - my application crashes.

The problem is - I don't quite understand how to properly add these buttons on my main layout and then use them.

So, I dont get it... Am I not properly adding these sign in/out buttons, or is there something wrong with my layout? How do you actually add these buttons to the main layout? I didnt find any source in the internet that provides a complete implementation of these buttons, only pieces of code.

May be its got something to do with the view? I dont get it - if Im implementing View.OnClickListener, then how do these buttons know to what view they are assigned to... As I created another view for ads, but its a special view. Im just lost :(

Here is my main layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- sign-in button -->
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<!-- sign-out button -->
<Button
android:id="@+id/sign_out_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Out"
android:visibility="gone" />

</LinearLayout>

And thats what I do in my main activity:

public class AndroidLauncher extends AndroidApplication implements AdInterface, GameHelperListener, ActionResolver, View.OnClickListener {

int launched = 0;

Intent intent;

private final String AD_UNIT_ID = "------";

private final int SHOW_ADS = 1;
private final int HIDE_ADS = 0;

protected AdView adView;

private GameHelper gameHelper;

ConnectivityManager cm;

NetworkInfo ni;

protected Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
        case SHOW_ADS:
            adView.setVisibility(View.VISIBLE);
            //AdRequest adRequest = new AdRequest.Builder().build();
            //adView.loadAd(adRequest);
            break;
        case HIDE_ADS:
            adView.setVisibility(View.GONE);
            break;
        }
    }
};

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

    if (launched == 0){


    setContentView(R.layout.main);

    findViewById(R.id.sign_in_button).setOnClickListener(this);
    findViewById(R.id.sign_out_button).setOnClickListener(this);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    intent = new Intent(this, VideoActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();

    initialize(new Game(this, this), cfg);

    if (gameHelper == null) {

        gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
        gameHelper.enableDebugLog(true);

        }
    gameHelper.setup(this);


    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);
    adView.setId(12345); // this is an arbitrary id, allows for relative positioning in createGameView()

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, 
            FrameLayout.LayoutParams.WRAP_CONTENT, 
            Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);

    adView.setLayoutParams(params);
    adView.setBackgroundColor(Color.BLACK);


    addContentView(adView, params);


    startAdvertising(adView);

    launched = 1;
    }


//*@Override
//*public void onStart(){
//* super.onStart();
//* gameHelper.onStart(this);
//*}

@Override
public void onStop(){
    super.onStop();
    gameHelper.onStop();
}

@Override
public void onActivityResult(int request, int response, Intent data) {
    super.onActivityResult(request, response, data);
    gameHelper.onActivityResult(request, response, data);
}

private void startAdvertising(AdView adView) {
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
  }

@Override
public void showAds(boolean show) {
    // TODO Auto-generated method stub
    handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
}

@Override
public boolean getSignedInGPGS() {
    // TODO Auto-generated method stub
    //return false;
    return gameHelper.isSignedIn();
}

@Override
public void loginGPGS() {
    // TODO Auto-generated method stub
    try {
        runOnUiThread(new Runnable(){
            public void run() {
                gameHelper.beginUserInitiatedSignIn();
            }
        });
    } catch (final Exception ex) {
    }

}

@Override
public void submitScoreGPGS(int score) {
    // TODO Auto-generated method stub

}

@Override
public void unlockAchievementGPGS(String achievementId) {
    // TODO Auto-generated method stub

    Games.Achievements.unlock(gameHelper.getApiClient(), achievementId);
    //Games.Achievements.

}

//@Override
public void revealAchievementGPGS(String achievementId) {
    // TODO Auto-generated method stub

    Games.Achievements.reveal(gameHelper.getApiClient(), achievementId);
    //Games.Achievements.

}

@Override
public void getLeaderboardGPGS() {
    // TODO Auto-generated method stub

}

@Override
public void getAchievementsGPGS() {
    // TODO Auto-generated method stub

    if (gameHelper.isSignedIn()) {
             startActivityForResult(Games.Achievements.getAchievementsIntent(gameHelper.getApiClient()), 101);
        }
        else if (!gameHelper.isConnecting()) {
        loginGPGS();
        }

}

@Override
public void onSignInFailed() {
    // TODO Auto-generated method stub

    // Sign in has failed. So show the user the sign-in button.
    findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
    findViewById(R.id.sign_out_button).setVisibility(View.GONE);

}

@Override
public void onSignInSucceeded() {
    // TODO Auto-generated method stub
    //loginGPGS();

    // show sign-out button, hide the sign-in button
    findViewById(R.id.sign_in_button).setVisibility(View.GONE);
    findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);

    // (your code here: update UI, enable functionality that depends on sign in, etc)

}

public boolean isNetworkConnected() {
      cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      ni = cm.getActiveNetworkInfo();
      if (ni == null) {
       // There are no active networks.
       return false;
      } else
       return true;
     }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if (v.getId() == R.id.sign_in_button) {
        // start the asynchronous sign in flow
        gameHelper.beginUserInitiatedSignIn();
    }
    else if (v.getId() == R.id.sign_out_button) {
        // sign out.
        gameHelper.signOut();

        // show sign-in button, hide the sign-out button
        findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
        findViewById(R.id.sign_out_button).setVisibility(View.GONE);
    }

}

}

The application is crashing because you do not have an android:id associated with the LinearLayout (in this case you'd need to add android:id="@+id/main")

When you're trying to reference these to hide them, they aren't actually being shown since the layout that is being created isn't the one you are expecting and it will crash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM