gpt4 book ai didi

java - NoClassDefFoundError 和 ExceptionInitializeError

转载 作者:行者123 更新时间:2023-12-02 03:00:57 26 4
gpt4 key购买 nike

当我运行应该使用 mongolab 连接到 mongo 数据库的代码时,我不断收到以下错误。我还有一个 try catch ,它导出的错误是“ExceptionInitializeError”。请帮忙:(

 FATAL EXCEPTION: main
Process: com.example.chadedwards.projectichnaea, PID: 8451
java.lang.NoClassDefFoundError: com/mongodb/connection/ClientMetadataHelper
at com.mongodb.connection.InternalStreamConnectionFactory.<init>(InternalStreamConnectionFactory.java:41)
at com.mongodb.connection.DefaultClusterableServerFactory.create(DefaultClusterableServerFactory.java:68)
at com.mongodb.connection.BaseCluster.createServer(BaseCluster.java:360)
at com.mongodb.connection.SingleServerCluster.<init>(SingleServerCluster.java:54)
at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:114)
at com.mongodb.Mongo.createCluster(Mongo.java:744)
at com.mongodb.Mongo.createCluster(Mongo.java:728)
at com.mongodb.Mongo.<init>(Mongo.java:293)
at com.mongodb.Mongo.<init>(Mongo.java:288)
at com.mongodb.MongoClient.<init>(MongoClient.java:192)
at com.mongodb.MongoClient.<init>(MongoClient.java:168)
at com.example.chadedwards.projectichnaea.BusAlarmActivity.onCreate(BusAlarmActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

我的等级

    apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
multiDexEnabled true
applicationId "com.example.chadedwards.projectichnaea"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.google.android.gms:play-services-maps:10.0.1'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'org.mongodb:mongodb-driver:3.4.2'
testCompile 'junit:junit:4.12'
}

和我的代码

package com.example.chadedwards.projectichnaea;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;

import java.util.Arrays;

public class BusAlarmActivity extends Activity {

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


try {

//MongoClient mongoClient = new MongoClient("ds063546.mlab.com", 63546);

String userName = "admin";
String database = "ichnaea";

char[] password = new char[] {'W', 'e', 'l', 'c', 'o', 'm' , 'e', '1', '2', '3'};
MongoCredential credential = MongoCredential.createCredential(userName, database, password);
MongoClient mongoClient = new MongoClient(new ServerAddress("ds063546.mlab.com", 63546), Arrays.asList(credential));


MongoDatabase db = mongoClient.getDatabase("test");

DBCollection coll = (DBCollection) db.getCollection("testCollection");

BasicDBObject doc = new BasicDBObject("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("info", new BasicDBObject("x", 203).append("y", 102));
coll.insert(doc);




}
catch(ExceptionInInitializerError ex){

Toast.makeText(this, ex.toString() , Toast.LENGTH_SHORT).show();

}

}
}

编辑 - 经过更多故障排除后,问题似乎从这一行开始 MongoClient mongoClient = new MongoClient

最佳答案

您缺少 mongodb-driver 运行的一些依赖项。

将这些行添加到您的 gradle.build 中:

compile group: 'org.mongodb', name: 'mongodb-driver-core', version: '3.4.2'
compile group: 'org.mongodb', name: 'bson', version: '3.4.2'

将它们放在当前依赖项之后。

关于java - NoClassDefFoundError 和 ExceptionInitializeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42370592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com