2016-07-26 105 views
1

我正在關注instruction以在Android Studio中設置CouchBase。CouchBase - 在Android Studio中找不到JavaContext

問題是JavaContext似乎並不存在。任何想法爲什麼?

private void test(){ 
    // Enable logging 
    Logger log = Logger.getLogger("app"); 
    log.setLevel(Level.ALL); 
    JavaContext context = new JavaContext(); // THIS LINE DOES NOT COMPILE 
    // Create a manager 
      Manager manager = null; 
      try { 
       manager = new Manager(context, Manager.DEFAULT_OPTIONS); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
    // Create or open the database named app 
      Database database = null; 
      try { 
       database = manager.getDatabase("app"); 
      } catch (CouchbaseLiteException e) { 
       e.printStackTrace(); 
      } 
    // The properties that will be saved on the document 
      Map<String, Object> properties = new HashMap<String, Object>(); 
      properties.put("title", "Couchbase Mobile"); 
      properties.put("sdk", "Java"); 
    // Create a new document 
      Document document = database.createDocument(); 
    // Save the document to the database 
      try { 
       document.putProperties(properties); 
      } catch (CouchbaseLiteException e) { 
       e.printStackTrace(); 
      } 
    // Log the document ID (generated by the database) 
    // and properties 
    log.info(String.format("Document ID :: %s", document.getId())); 
    log.info(String.format("Learning %s with %s", (String) document.getProperty("title"), (String) document.getProperty("sdk"))); 
} 

這裏是我的gradle產出:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.3" 

    defaultConfig { 
     applicationId "com.woxthebox.draglistview.sample" 
     minSdkVersion 11 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    // workaround for "duplicate files during packaging of APK" issue 
// see https://groups.google.com/d/msg/adt-dev/bl5Rc4Szpzg/wC8cylTWuIEJ 
    packagingOptions { 
     exclude 'META-INF/ASL2.0' 
     exclude 'META-INF/LICENSE' 
     exclude 'META-INF/NOTICE' 
    } 
} 

repositories { 
    jcenter() 
    maven { 
     url "http://files.couchbase.com/maven2/" 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile project(':library') 
    compile 'com.android.support:appcompat-v7:23.3.0' 
    compile 'com.android.support:cardview-v7:23.3.0' 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    //compile 'com.android.support:appcompat-v7:22.1.1' 
    compile 'com.couchbase.lite:couchbase-lite-android:1.3.0' 
} 

編輯:

一些上場後,各地,我發現此代碼:

private void test(){ 
     // Enable logging 
     Logger log = Logger.getLogger("app"); 
     log.setLevel(Level.ALL); 
     // JavaContext context = new JavaContext(); 

     // Create a manager 
       Manager manager = null; 
       try { 
        manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
     // Create or open the database named app 
       Database database = null; 
       try { 
        database = manager.getDatabase("app"); 
       } catch (CouchbaseLiteException e) { 
        e.printStackTrace(); 
       } 
     // The properties that will be saved on the document 
       Map<String, Object> properties = new HashMap<String, Object>(); 
       properties.put("title", "Couchbase Mobile"); 
       properties.put("sdk", "Java"); 
     // Create a new document 
       Document document = database.createDocument(); 
     // Save the document to the database 
       try { 
        document.putProperties(properties); 
       } catch (CouchbaseLiteException e) { 
        e.printStackTrace(); 
       } 
     // Log the document ID (generated by the database) 
     // and properties 
     log.info(String.format("Document ID :: %s", document.getId())); 
     log.info(String.format("Learning %s with %s", (String) document.getProperty("title"), (String) document.getProperty("sdk"))); 
    } 

有人能解釋什麼是錯的原來的指示?

+1

我有Couchbase文檔質量很差的經驗。它看起來像根據Github項目在主分支中不存在「JavaContext」。所以,請嘗試從這裏開始。 http://developer.couchbase.com/documentation/mobile/1.2/develop/training/build-first-android-app/starter-code-android/index.html –

+2

爲此抱歉,它已在https:/ /今天更新/github.com/couchbaselabs/couchbase-mobile-portal/commit/84713c4ed1659c365f06b9c7df41cb2b0778f50e,現在應該發佈。 – jamiltz

回答

2

有人可以解釋原指令有什麼問題嗎?

可能編寫文檔的人不知道Android Couchbase Lite的AndroidContext

對於標準的Java(embedded?)項目,實際上有一個JavaContext

Github上的入門代碼實際上也使用AndroidContext,因此頁面錯誤。

看到這裏的(貌似正確的)Couchbase Android - Getting Started Guide

+0

謝謝,這是一個很好的教訓。請注意文檔... – jhegedus

+0

文檔已更新。 – Hod

相關問題