2016-07-01 11 views
1

Google Playサービスに接続する方法を試すサンプルアプリを作成しました。携帯電話やタブレットで試してみると、アプリを開くとクラッシュします。Google Playサービスに接続すると端末がクラッシュする

私は、client.connect()を使って接続しようとしたときにエラーが発生していることを知っています。これは、コメントが付いてもアプリケーションがクラッシュしないからです。

主なアクティビティは次のとおりです。正しくコンパイルしてからインポートを省略します。

public class ry extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener { 


private GoogleApiClient client; 
//GoogleApiClient 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    client = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(Games.API).addScope(Games.SCOPE_GAMES) 
      .build(); 

} 

@Override 
public void onStart() { 
    super.onStart(); 
    client.connect(); // <- Error line 


} 

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

} 

public void onConnectionSuspended(int cause) { 
    // We are not connected anymore! 
    Log.i("app", "connectionsuspended"); 
} 

public void onConnected(Bundle connectionHint) { 

} 

public void onConnectionFailed(ConnectionResult result) { 
    // We tried to connect but failed! 


    try { 
     result.startResolutionForResult(this, 49404); 
    } catch (Exception e) { 
     Log.i("app", e.toString()); 
    } 

} 

} 

そしてまた私のgradle.build:

[...] 
dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:24.0.0' 
compile 'com.google.android.gms:play-services-games:9.2.0' 
} 
[...] 

すべてのヘルプは大歓迎だろう。

更新、エラーはなく、警告のみが表示され、次のような関連性があるため、関連するログエラーの行を投稿します。

07-04 12:20:15.840 5124-5192/com.example.internet.connectiontry W/GooglePlayServicesUtil: Google Play services out of date. Requires 9256000 but found 9080270 

アップデート2:

--------- beginning of crash 
07-04 19:35:14.788 2245-2245/com.example.internet.connectiontry  E/AndroidRuntime: FATAL EXCEPTION: main 
[...] 
    Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist. You must have the following declaration within the <application> element:  <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 

これが私のマニフェストです:

compile 'com.google.android.gms:play-services:4.2.+' 

を使用する代わりに、それはトリックを行い、しかし、それが原因でクラッシュするようですファイル:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    package="com.example.internet.connectiontry"> 

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


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

     <activity android:name=".MainActivity"> 
      <meta-data android:name="com.google.android.gms.version"  android:value="@integer/google_play_services_version" /> 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

      </intent-filter> 
     </activity> 

    </application> 


</manifest> 

インターネット情報によると、そのメタデータタグはアプリケーションタグの最初の子である必要がありますが、何回かポジションを試してみると機能しません。

+0

更新されたAndroidスタジオがありますか? –

+0

また、あなたのアプリで更新されたGoogleサービスもありますか? – Drv

+0

取得したログエラーを投稿できますか? –

答えて

0

これを試してみてください:Googleがサービスを再生するかどうか

@Override 
public void onStart() { 
    super.onStart(); 
     if(client != null){ 
     client.connect(); 
     } 
} 

チェックをデバイスにインストールされています。

GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); 
     int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(this); 
     if (resultCode == ConnectionResult.SUCCESS) { 
      // do your task 
     } else{ 
      Toast.makeText(this, "Google Play Services required!!", Toast.LENGTH_LONG).show(); 
     } 
+0

私は試しましたが動作しません。 – NUN

+0

ログエラーを投稿できますか? –

+0

Google Playサービスが端末にインストールされているかどうか確認しましたか?私の更新された答えを確認してください –

0

があなたのonConnectionFailed@OverrideをごonCreate方法

if (client == null) { 
client = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(Games.API).addScope(Games.SCOPE_GAMES) 
      .build(); 
} 

にこれを追加し、追加します方法

関連する問題