2013-10-21 23 views
3

我想。當我正在乳寧它拋出異常是的NoClassDefFoundError爲GooglePlayServicesUtil的Android

10-21 18:46:14.991: E/AndroidRuntime(4817): FATAL EXCEPTION: main 
10-21 18:46:14.991: E/AndroidRuntime(4817): java.lang.NoClassDefFoundError: com.google.android.gms.common.GooglePlayServicesUtil 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at com.corporate.test.HomePage.checkPlayServices(HomePage.java:211) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at com.corporate.test.HomePage.onCreate(HomePage.java:140) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at android.app.Activity.performCreate(Activity.java:5020) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at android.app.ActivityThread.access$600(ActivityThread.java:149) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at android.os.Looper.loop(Looper.java:153) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at android.app.ActivityThread.main(ActivityThread.java:4987) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at java.lang.reflect.Method.invoke(Method.java:511) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 
10-21 18:46:14.991: E/AndroidRuntime(4817):  at dalvik.system.NativeStart.main(Native Method) 

應用和我的活動代碼來實現谷歌雲端通訊是

import java.io.IOException; 
import java.util.concurrent.atomic.AtomicInteger; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.app.Dialog; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.pm.PackageInfo; 
import android.content.pm.PackageManager.NameNotFoundException; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.gcm.GoogleCloudMessaging; 
import com.nextra.utils.AppState; 

public class HomePage extends Activity { 
    RelativeLayout mytrips_relative, approval_relative, 
      view_transations_relative; 
    Button backViewTransations, continueViewTransactions; 
    ImageView logout; 
    final Context context = this; 
    public String TAG = "HomePage"; 
    boolean isConnected; 
    /* 
    * GCM Client Variables 
    * */ 
    public static final String EXTRA_MESSAGE = "message"; 
    public static final String PROPERTY_REG_ID = "registration_id"; 
    private static final String PROPERTY_APP_VERSION = "appVersion"; 
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; 

    String SENDER_ID = "XXXX"; 
    TextView mDisplay; 
    GoogleCloudMessaging gcm; 
    AtomicInteger msgId = new AtomicInteger(); 
    Context contextGCM; 

    String regid; 

    @SuppressLint("NewApi") 

    /* 
    * GCM Client Variables END 
    * */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_home_page); 



     /* 
     * GCM PlayServices On 
     * */ 
     contextGCM = getApplicationContext(); 

     // Check device for Play Services APK. If check succeeds, proceed with 
     // GCM registration. 
     if (checkPlayServices()) { 
      gcm = GoogleCloudMessaging.getInstance(this); 
      regid = getRegistrationId(contextGCM); 
      Log.d("Registration id ", regid + ""); 
      if (regid.isEmpty()) { 
       registerInBackground(); 
      } 
     } else { 
      Log.i(TAG, "No valid Google Play Services APK found."); 
     } 
    } 





    @Override 
    protected void onResume() { 
     super.onResume(); 
     // Check device for Play Services APK. 
     checkPlayServices(); 
    } 

    /** 
    * Check the device to make sure it has the Google Play Services APK. If it 
    * doesn't, display a dialog that allows users to download the APK from the 
    * Google Play Store or enable it in the device's system settings. 
    */ 
    private boolean checkPlayServices() { 
     int resultCode = GooglePlayServicesUtil 
       .isGooglePlayServicesAvailable(this); 
     if (resultCode != ConnectionResult.SUCCESS) { 
      if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { 
       GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
         PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
      } else { 
       Log.i(TAG, "This device is not supported."); 
       finish(); 
      } 
      return false; 
     } 
     return true; 
    } 

    /** 
    * Stores the registration ID and the app versionCode in the application's 
    * {@code SharedPreferences}. 
    * 
    * @param context 
    *   application's context. 
    * @param regId 
    *   registration ID 
    */ 
    private void storeRegistrationId(Context context, String regId) { 
     final SharedPreferences prefs = getGcmPreferences(context); 
     int appVersion = getAppVersion(context); 
     Log.i(TAG, "Saving regId on app version " + appVersion); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString(PROPERTY_REG_ID, regId); 
     editor.putInt(PROPERTY_APP_VERSION, appVersion); 
     editor.commit(); 
    } 

    /** 
    * Gets the current registration ID for application on GCM service, if there 
    * is one. 
    * <p> 
    * If result is empty, the app needs to register. 
    * 
    * @return registration ID, or empty string if there is no existing 
    *   registration ID. 
    */ 
    @SuppressLint("NewApi") 
    private String getRegistrationId(Context context) { 
     final SharedPreferences prefs = getGcmPreferences(context); 
     String registrationId = prefs.getString(PROPERTY_REG_ID, ""); 
     if (registrationId.isEmpty()) { 
      Log.i(TAG, "Registration not found."); 
      return ""; 
     } 
     // Check if app was updated; if so, it must clear the registration ID 
     // since the existing regID is not guaranteed to work with the new 
     // app version. 
     int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, 
       Integer.MIN_VALUE); 
     int currentVersion = getAppVersion(context); 
     if (registeredVersion != currentVersion) { 
      Log.i(TAG, "App version changed."); 
      return ""; 
     } 
     return registrationId; 
    } 

    /** 
    * Registers the application with GCM servers asynchronously. 
    * <p> 
    * Stores the registration ID and the app versionCode in the application's 
    * shared preferences. 
    */ 
    private void registerInBackground() { 
     new AsyncTask<Void, Void, String>() { 
      @Override 
      protected String doInBackground(Void... params) { 
       String msg = ""; 
       try { 
        if (gcm == null) { 
         gcm = GoogleCloudMessaging.getInstance(contextGCM); 
        } 
        regid = gcm.register(SENDER_ID); 
        Log.d("registration Id", regid + ""); 
        msg = "Device registered, registration ID=" + regid; 

        // You should send the registration ID to your server over 
        // HTTP, so it 
        // can use GCM/HTTP or CCS to send messages to your app. 
        sendRegistrationIdToBackend(); 

        // For this demo: we don't need to send it because the 
        // device will send 
        // upstream messages to a server that echo back the message 
        // using the 
        // 'from' address in the message. 

        // Persist the regID - no need to register again. 
        storeRegistrationId(contextGCM, regid); 
       } catch (IOException ex) { 
        msg = "Error :" + ex.getMessage(); 
        // If there is an error, don't just keep trying to register. 
        // Require the user to click a button again, or perform 
        // exponential back-off. 
       } 
       return msg; 
      } 

      @Override 
      protected void onPostExecute(String msg) { 
       mDisplay.append(msg + "\n"); 
      } 
     }.execute(null, null, null); 
    } 



    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
    } 




} 

看,我安裝Google Play Services並且還完成了構建路徑仍然我收到錯誤。

+0

你是如何配置你的構建路徑的? –

+0

是否在您的工作空間中導入了庫並添加了對它的引用。 –

+0

我做了所有需要的但它不起作用 – Developer

回答

0

檢查您是否導入了Google Play Services項目,並將其作爲從屬庫添加到項目中。轉到導入的項目屬性 - > Android部分,並選中「Is Library」框。

然後將其添加爲項目依賴項。如果您使用的是Eclipse,請轉至您的項目「屬性」 - >「Android」部分,然後在「庫」部分中添加導入的Google Play Services項目。

此外,修復您的項目屬性和清潔也可以幫助你。嘗試右鍵單擊您的項目 - > Android工具 - >修復項目屬性。然後,清理你的項目並嘗試運行你的項目。

-2

如果在運行時找不到您的代碼的類,則發生此錯誤。通常這意味着某些代碼在打包和/或部署代碼時出錯。檢查IDE的設置,嘗試重建整個項目。

+0

你說的很明顯從錯誤信息。爲了有幫助,您需要提供有關缺少哪個庫以及如何添加它的建議。 –

1

我在Android Studio 0.3.1下有同樣的錯誤。在文件的build.gradle,我補充說:「播放服務」(V3.2.0或更高),以獲得:

dependencies { 
    compile 'com.android.support:support-v4:18.0.0' 
    compile 'com.google.android.gms:play-services:3.2.+' 
} 

我不得不重新啓動Android的工作室在此之前最後的工作。使用工具> Android> SDKManager確保已安裝Google Play「額外」。

1

我嘗試了所有我能找到的建議,至少重新創建兩次庫項目。

然後Eclipse耗盡內存,很有趣,因爲我有64 GB的RAM。

我決定重新啓動Windows。當一切都恢復正常時,它就起作用了。 :-P

相關問題