我一直在试图弄清楚为什么我收到一个错误,指出找不到该方法,但一切似乎都井井有条(至少是会导致此错误的事情)。
我的安卓课程:
package com.liamw.root.androididchanger;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DebugUserActivity extends Activity {
Button button;
TextView logcat;
static {
System.loadLibrary("sqlite");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debug);
button = (Button) findViewById(R.id.button1);
logcat = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
runSQL("example.sqlite3", "SELECT Value FROM MyTable");
}
});
/*try {
Runtime.getRuntime().exec("logcat -c").waitFor();
Process process = Runtime.getRuntime().exec("logcat -v long *:*");
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
while (true) {
String nextLine = reader.readLine();
logcat.setText(logcat.getText().toString() + "\n" + nextLine);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
}
public native void runSQL(String path, String query);
}
本机文件(idchanger.c):
#include <jni.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <android/log.h>
#include <../sqlite3.c>
#include <../sqlite3.h>
#ifndef LOG_TAG
#define LOG_TAG "idchanger.c"
#endif
#define SQLITE_NDK_VFS_NAME "name"
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
void Java_com_liamw_root_androididchanger_DebugUserActivity_runSQL(JNIEnv * env,
jobject this, jstring query, jstring path) {
sqlite3 *db;
sqlite3_stmt *stmt;
jboolean isCopy;
const char * rQuery = (*env)->GetStringUTFChars(env, query, &isCopy);
const char * rPath = (*env)->GetStringUTFChars(env, path, &isCopy);
if (sqlite3_open_v2(rPath, &db, SQLITE_OPEN_READWRITE, SQLITE_NDK_VFS_NAME) == SQLITE_OK) {
LOGI("Database opened OK");
if (sqlite3_prepare_v2(db, rQuery, -1, &stmt, NULL)
== SQLITE_OK)
{
LOGI("Table opened OK");
int err;
while ((err = sqlite3_step(stmt)) == SQLITE_ROW)
{
LOGI("Value: %s\n\n", sqlite3_column_text(stmt, 0));
}
if (err != SQLITE_DONE)
{
LOGE("Query failed: %s\n", sqlite3_errmsg(db));
}
LOGI("Finalise...");
sqlite3_finalize(stmt);
}
else
{
LOGE("Could't execute query: %s\n", sqlite3_errmsg(db));
}
}
else {
LOGE("Can't open database: %s\n", sqlite3_errmsg(db));
}
LOGI("Close connection");
sqlite3_close(db);
}
错误:
09-07 20:48:07.793: E/AndroidRuntime(21193): FATAL EXCEPTION: main
09-07 20:48:07.793: E/AndroidRuntime(21193): java.lang.UnsatisfiedLinkError: Native method not found: com.liamw.root.androididchanger.DebugUserActivity.runSQL:(Ljava/lang/String;Ljava/lang/String;)V
09-07 20:48:07.793: E/AndroidRuntime(21193): at com.liamw.root.androididchanger.DebugUserActivity.runSQL(Native Method)
09-07 20:48:07.793: E/AndroidRuntime(21193): at com.liamw.root.androididchanger.DebugUserActivity$1.onClick(DebugUserActivity.java:34)
09-07 20:48:07.793: E/AndroidRuntime(21193): at android.view.View.performClick(View.java:4211)
09-07 20:48:07.793: E/AndroidRuntime(21193): at android.view.View$PerformClick.run(View.java:17362)
09-07 20:48:07.793: E/AndroidRuntime(21193): at android.os.Handler.handleCallback(Handler.java:725)
09-07 20:48:07.793: E/AndroidRuntime(21193): at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 20:48:07.793: E/AndroidRuntime(21193): at android.os.Looper.loop(Looper.java:137)
09-07 20:48:07.793: E/AndroidRuntime(21193): at android.app.ActivityThread.main(ActivityThread.java:5227)
09-07 20:48:07.793: E/AndroidRuntime(21193): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 20:48:07.793: E/AndroidRuntime(21193): at java.lang.reflect.Method.invoke(Method.java:511)
09-07 20:48:07.793: E/AndroidRuntime(21193): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
09-07 20:48:07.793: E/AndroidRuntime(21193): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
09-07 20:48:07.793: E/AndroidRuntime(21193): at dalvik.system.NativeStart.main(Native Method)
一切似乎都是为了我,但错误是什么?
编辑:
安卓.mk:
LOCAL_PATH := $(call my-dir)
$(LOCAL_PATH)/../sqlite3.c:
$(MAKE) -C $(@:%/sqlite3.c=%) sqlite3.c
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog -landroid
LOCAL_MODULE := sqlite
LOCAL_SRC_FILES := ../sqlite3.c idchanger.c
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_CFLAGS := -DSQLITE_THREADSAFE=1
include $(BUILD_STATIC_LIBRARY)
编辑2:
啊哈!这被添加到 logcat 中:
09-09 19:59:28.605: D/dalvikvm(9696): No JNI_OnLoad found in /system/lib/libsqlite.so 0x40e46360, skipping init
编辑 3:我刚刚注意到,在我制作的测试应用程序中,logcat 中有几行表明它试图加载库,然后是 ndk 库文件......
我在这里看不到这些线....