2023-01-19 386
我想从本机代码调用Java方法.我遵循以下步骤:
在Android
中调用C ++的Java方法
但是,我的应用程序在称为Java函数的地方崩溃.
我的Java代码如下所示:
package com.example.jnitry;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MYAPP","Ocreate entered....");
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mainfunc(8);
}
});
}
public void display(byte[] byt){
Log.d("MYAPP", "display() is entered....");
for(int i=0;i<byt.length;i++)
Log.d("MYAPP", "byt["+i+"]="+byt[i]);
Log.d("MYAPP", "display finished");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public static native void mainfunc(int n);
static
{
System.loadLibrary("mylib");
}
}
我的本机代码如下所示:
#include <jni.h>
#include "com_example_jnitry_MainActivity.h"
#include <android/log.h>
#include <stdio.h>
JNIEXPORT void JNICALL Java_com_example_jnitry_MainActivity_mainfunc
(JNIEnv *env, jclass obj, jint n){
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","mainfunction entered...",NULL);
int i;
unsigned char arr[10];
jbyteArray bArray=(*env)->NewByteArray(env,n);
jclass cls = (*env)->FindClass(env, "com/example/jnitry/MainActivity");
jmethodID mid = (*env)->GetMethodID(env, cls, "display", "([B)V");
if (mid == 0){
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","mid==0",NULL);
return;
}
if(bArray==NULL)
{ __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","bArray==NULL...",NULL);
return ;
}
for(i=0;i<n;i++){
arr[i]=i;
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Iteration number:%d",i);
}
(*env)->SetByteArrayRegion(env,bArray,0,n,arr);
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","bArray successfully created...",NULL);
(*env)->CallVoidMethod(env, obj, mid, bArray);
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Returned from disp() java function.....",NULL);
}
我的应用在打印日志消息后崩溃:
02-14 15:37:06.814: D/MYAPP(11584): bArray successfully created...
任何人都可以告诉我原因.并为此提供解决方案.预先感谢.
注意:我已经尝试了 callVoidMedThoda(), callbyTemethod(), callobjectMethod()结果是相同的.
您的本机方法mainFunc是一种静态方法 – 它没有有效的this指针.同时,display是一种实例方法 – 它需要一个.
根据JNI规则,mainFunc的第二个参数实际上是MainActivity的类对象指针.声明mainFunc在java侧是非静态的,这将起作用.
我可以给你这个.有用.但这是C ++. (所以吃了标签 – 对不起.)
// it returns NULL in the case of an exception
// the returned memory is calloc()'d; it's the caller's responsibility to free() it.
char* changeEncoding(const char*source, int len, int direction)
{
JNIEnv* env = threadUnsafeInfo.env;
jobject obj = threadUnsafeInfo.obj;
if (!source) {
JNU_ThrowByName(env, "java/lang/NullPointerException", 0);
return NULL;
}
jbyteArray srcArray = env->NewByteArray(len);
jclass cls = env->FindClass("com/xxx/Yyy");
jmethodID mid = env->GetMethodID(cls, "convert", "([BI)[B");
if (mid != NULL && srcArray != NULL) {
env->SetByteArrayRegion(srcArray, 0, len, (jbyte*)source);
env->ExceptionClear();
jbyteArray resArray = (jbyteArray)env->CallObjectMethod(obj, mid, srcArray, direction);
if(env->ExceptionOccurred()) {
DLOG("exception in convert ([BI)[B");
env->ExceptionDescribe();
//env->ExceptionClear(); // ??
return NULL;
}
int resultLen = env->GetArrayLength(resArray);
char* result = (char*)calloc(2 + resultLen,1); // why 2: a bit of healthy paranoia ain't gonna hurt anyone
if (result == 0) {
JNU_ThrowByName(env, "java/lang/OutOfMemoryError", 0);
return NULL;
}
env->GetByteArrayRegion(resArray, 0, resultLen, (jbyte *)result);
env->DeleteLocalRef(cls);
env->DeleteLocalRef(resArray);
env->DeleteLocalRef(srcArray);
return result;
} else {
JNU_ThrowByName(env, "java/lang/NullPointerException", 0);
myassert(("method id = 0",0));
}
return NULL;
}
afaik callVoidMethod不采用ENV参数.至少在C ++中.
以上所述是小编给大家介绍的从本地代码调用java函数出错,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/25798.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态