我正在尝试按照此处的建议实施Google Play游戏服务登录/退出按钮:https://developers.google.com/games/services/training/signin
但无论何时
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
或
findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
被调用 - 我的应用程序崩溃了。
问题是 - 我不太了解如何在我的主布局上正确添加这些按钮然后再使用它们。
所以,我不明白......我没有正确添加这些登录/退出按钮,或者我的布局有问题吗?你如何将这些按钮添加到主布局?我没有在互联网上找到任何提供这些按钮的完整实现的源代码,只提供了一些代码。
可能是它与视图有关吗?我不明白 - 如果我实现了View.OnClickListener,那么这些按钮如何知道它们分配给哪个视图...当我为广告创建另一个视图时,它是一个特殊的视图。我刚丢了:(
以下是我的主要布局:
<?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>
这就是我在主要活动中所做的事情:
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);
}
}
}
答案 0 :(得分:0)
应用程序崩溃是因为你没有与LinearLayout相关联的android:id(在这种情况下你需要添加android:id =&#34; @ + id / main&#34;)< / p>
当您尝试引用这些内容以隐藏它们时,它们实际上并未显示,因为正在创建的布局不是您期望的布局并且会崩溃。