我有一些问题。
简单地说,输入启动代码,
这里, https://github.com/pardom/Ollie
这是我的代码。
第一个gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "example.com.ollietest001"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.michaelpardo:ollie:0.3.1'
provided 'com.michaelpardo:ollie-compiler:0.3.1'
}
第二,MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import ollie.Ollie;
public class MainActivity extends Activity {
public static final String Test = "test.db";
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
Ollie.with(mContext)
.setName(Test)
.setVersion(1)
.setLogLevel(Ollie.LogLevel.FULL)
.setCacheSize(1024)
.init();
}
}
它刚刚返回NullPointerException。
我不明白,为什么?
Caused by: java.lang.NullPointerException
at ollie.Ollie$DatabaseHelper.executeCreate(Ollie.java:336)
at ollie.Ollie$DatabaseHelper.onCreate(Ollie.java:317)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at ollie.Ollie.init(Ollie.java:143)
at ollie.Ollie$Builder.init(Ollie.java:298)
at example.com.dbtest001.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5179)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4961)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
从未使用过该库,但堆栈跟踪和源代码很容易理解:
at ollie.Ollie$DatabaseHelper.executeCreate(Ollie.java:336)
是
for (ModelAdapter modelAdapter : sAdapterHolder.getModelAdapters()) {
NPE必须是sAdapterHolder
是null
。它在init()
中的try块中初始化:
try {
Class adapterClass = Class.forName(AdapterHolder.IMPL_CLASS_FQCN);
sAdapterHolder = (AdapterHolder) adapterClass.newInstance();
} catch (Exception e) {
if (sLogLevel.log(LogLevel.BASIC)) {
Log.e(TAG, "Failed to initialize.", e);
}
}
之后代码继续执行其他init。这似乎是图书馆中的一个错误,应该放弃那里的例外。
AdapterHolder.IMPL_CLASS_FQCN
:
public static final String IMPL_CLASS_PACKAGE = "ollie";
public static final String IMPL_CLASS_NAME = "AdapterHolderImpl";
public static final String IMPL_CLASS_FQCN = IMPL_CLASS_PACKAGE + "." + IMPL_CLASS_NAME;
这似乎是另一个错误:尝试按名称加载一个不存在的类。
建议:不要使用不适合您的库,除非您愿意花时间和精力调试和修复其内部。