尝试构建我的第二个jni android应用程序并坚持到某一点。
这是我的基本示例包
package com.example.angelica.myapplication;
public class SomeVars {
//load library
public int aPrivate;
public native void UpdateVars();
}
和我的jni实现
#include <jni.h>
extern "C"
JNIEXPORT void JNICALL
Java_com_example_angelica_myapplication_SomeVars_UpdateVars(JNIEnv *env, jobject instance) {
// TODO
jclass cls = getclass();
jfieldID fid = getField("aPrivate");
jint i = env->GetIntField(cls,fid);
i = i + 100;
env->SetIntField(cls, fid,i);
}
和我的MainActivity.java文件
public class MainActivity extends AppCompatActivity {
TextView tv;
SomeVars var = new SomeVars();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.sample_text);
var.aPrivate = 23;
if (var.aPrivate == 23) {
tv.setText("23");
}else {
tv.setText("Two Hunderd");
}
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
var.UpdateVars();
if (var.aPrivate == 23) {
tv.setText("23");
}else {
tv.setText("Changed");
}
}
});
}
}
项目构建成功,并正确启动。但是当单击按钮时,项目异常停止。我试着看一下logcat日志。但无法理解错误背后的原因。发布logcat的几行
04-16 13:34:56.171 3949-3949/com.example.angelica.myapplication A/art: art/runtime/java_vm_ext.cc:470] JNI DETECTED ERROR IN APPLICATION: jfieldID int com.example.angelica.myapplication.SomeVars.aPrivate not valid for an object of class java.lang.Class<com.example.angelica.myapplication.SomeVars>
04-16 13:34:56.171 3949-3949/com.example.angelica.myapplication A/art: art/runtime/java_vm_ext.cc:470] in call to GetIntField
04-16 13:34:56.171 3949-3949/com.example.angelica.myapplication A/art: art/runtime/java_vm_ext.cc:470] from void com.example.angelica.myapplication.SomeVars.UpdateVars()
04-16 13:34:56.171 3949-3949/com.example.angelica.myapplication A/art: art/runtime/java_vm_ext.cc:470] "main" prio=5 tid=1 Runnable
04-16 13:34:56.171 3949-3949/com.example.angelica.myapplication A/art: art/runtime/java_vm_ext.cc:470] | group="main" sCount=0 dsCount=0 obj=0x7449df60 self=0xa968b400
04-16 13:34:56.171 3949-3949/com.example.angelica.myapplication A/art: art/runtime/java_vm_ext.cc:470] | sysTid=3949 nice=-10 cgrp=default sched=0/0 handle=0xada11534
04-16 13:34:56.171 3949-3949/com.example.angelica.myapplication A/art: art/runtime/java_vm_ext.cc:470] | state=R schedstat=( 0 0 0 ) utm=4 stm=18 core=1 HZ=100
04-16 13:34:56.171 3949-3949/com.example.angelica.myapplication A/art: art/runtime/java_vm_ext.cc:470] | stack=0xbf5fe000-0xbf600000 stackSize=8MB
04-16 13:34:56.171 3949-3949/com.example.angelica.myapplication A/art: art/runtime/java_vm_ext.cc:470] | held mutexes= "mutator lock"(shared held)
我基本上试图实现当单击按钮时aPrivate的值更新并显示在textview中。从我的查找中可以清楚地看到所有错误都在jni实现中。
更新:
我已经跟踪了jni实现中两个位置发生的错误
jint i = env->GetIntField(cls,fid); //Error 1
env->SetIntField(cls, fid,200); //Error 2
如果我对这两行发表评论,则不会发生错误。
答案 0 :(得分:2)
我在设置SetIntField时有点错误,它应该使用实例设置字段。
env->SetIntField(cls, fid,200); //Error
env->SetIntField(instance, fid, 200); //SOLVED
现在每件事都运作良好