我正在尝试运行一个 android 应用程序。它应该包含 can-android 通信。我的项目叫做 CANAndroid。路径是 com.example.canandroid 。我目前正在使用这个java文件:package com.example.canandroid;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
public native void jinit(int type, int port, int irq);
public native void jreceive();
EditText et;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("BLA","Hier wird die App gestartet");
et = (EditText) findViewById(R.id.et1);
b1 =(Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jreceive();
//or jinit(0,0,0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
static{
System.loadLibrary("receive_test");
}
}
我的 receivetest.c 文件如下所示:
JNIEXPORT void JNICALL Java_com_example_CANAndroid_MainActivity_jinit(JNIEnv *env,jint nType, jint dwPort, jint wIrq){
//do sth
}
JNIEXPORT void JNICALL Java_com_example_CANAndroid_MainActivity_jreceive(){
//do sth
}
我的 receivetest.h 看起来像这样:#include
#ifndef _Included_com_example_CANAndroid_MainActivity
#define _Included_com_example_CANAndroid_MainActivity
#ifdef __cplusplus
extern "C"{
#endif
JNIEXPORT void JNICALL Java_com_example_CANAndroid_MainActivity_jinit
(JNIEnv *,jint, jint, jint);
JNIEXPORT void JNICALL Java_com_example_CANAndroid_MainActivity_jreceive();
#ifdef __cplusplus
}
#endif
#endif
我正在使用 ndk-toolchain 进行编译。所以我的 android.mk 看起来像这样
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=receive_test
LOCAL_SRC_FILE := receivetest.c
include $(BUILD_SHARED_LIBRARY)
该应用程序启动,但只要我按下按钮,我就会收到以下错误消息:
01-01 00:18:09.800: E/AndroidRuntime(852): FATAL EXCEPTION: main
01-01 00:18:09.800: E/AndroidRuntime(852): java.lang.UnsatisfiedLinkError: jinit
01-01 00:18:09.800: E/AndroidRuntime(852): at com.example.canandroid.MainActivity.jinit(Native Method)
01-01 00:18:09.800: E/AndroidRuntime(852): at com.example.canandroid.MainActivity$1.onClick(MainActivity.java:37)
01-01 00:18:09.800: E/AndroidRuntime(852): at android.view.View.performClick(View.java:3511)
01-01 00:18:09.800: E/AndroidRuntime(852): at android.view.View$PerformClick.run(View.java:14105)
01-01 00:18:09.800: E/AndroidRuntime(852): at android.os.Handler.handleCallback(Handler.java:605)
01-01 00:18:09.800: E/AndroidRuntime(852): at android.os.Handler.dispatchMessage(Handler.java:92)
01-01 00:18:09.800: E/AndroidRuntime(852): at android.os.Looper.loop(Looper.java:137)
01-01 00:18:09.800: E/AndroidRuntime(852): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-01 00:18:09.800: E/AndroidRuntime(852): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 00:18:09.800: E/AndroidRuntime(852): at java.lang.reflect.Method.invoke(Method.java:511)
01-01 00:18:09.800: E/AndroidRuntime(852): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-01 00:18:09.800: E/AndroidRuntime(852): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-01 00:18:09.800: E/AndroidRuntime(852): at dalvik.system.NativeStart.main(Native Method)
我已经在 stackoverflow 上搜索了几个威胁。它们都包含本机方法的拼写错误。我在我的程序中找不到任何这些,所以也许有人可以看到我的错误。