2014-09-19 52 views
0

我試着去實現谷歌玩遊戲服務登錄/退出鍵作爲其建議在這裏: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

此外,這些按鈕根本不顯示。我只是不小心,如何正確地將它們添加在佈局上,並在主要活動中使用它們::( – Redbu11 2014-09-19 13:56:12

回答

0

的應用程序,因爲你做的崩潰沒有與LinearLayout關聯的android:id(在這種情況下,您需要添加android:id =「@ + id/main」)

當您試圖引用這些來隱藏它們時,實際上並未顯示它們,因爲正在創建的佈局不是您期望的佈局,並且會崩潰。

+0

嗯,我的猜測是我的公共類AndroidLauncher擴展了AndroidApplication,而不是一個Activity。這應該是原因,因爲當我調用了setContentView(R.layout.main);它工作的很好,但是當我添加按鈕並嘗試調用它們時 - 它崩潰了。它沒有從佈局文件的名稱中獲得一個Id?在我的情況下,它的main.xml,所以我打電話給setContentView(R.layout.main);其中「.main」是一個id,是不是?或者它需要在.xml中設置一個Id來添加按鈕嗎? – Redbu11 2014-09-25 09:51:30

+0

不能檢查你的答案順便說一句,因爲我決定不在android特定的代碼中添加按鈕...所以我會認爲它是答案,因爲它聽起來合法xD – Redbu11 2014-09-25 10:04:19

相關問題