我从C ++触摸屏程序更改页面,需要同时从Android更改图像。但是当它(C ++ JNI)调用java JNI时,它会抛出" java.lang.NullPointerException"在JNIPage.java的setContentView()上。这是代码:
C ++ JNI:
void ofAppAndroidWindow::page(int wMethod){
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLog(OF_LOG_ERROR,"Failed to get the environment using GetEnv()");
return;
}
jclass localClass = env->FindClass("cc/openframeworks/androidEmptyExample/JNIPage");
javaClass = (jclass) env->NewGlobalRef(localClass);
if(!javaClass){
ofLog(OF_LOG_ERROR,"Failed to get the java class for JNIPage");
return;
}
jmethodID javaPageMethod = env->GetMethodID(javaClass,"changePage","(I)V");
if(!javaPageMethod){
ofLog(OF_LOG_ERROR,"Failed to get the java constructor for JNIPage");
return;
}
javaJNI = env->NewObject(javaClass,javaPageMethod);
if(!javaJNI){
ofLog(OF_LOG_ERROR,"Failed to create java JNIPage");
return;
}
javaJNI = (jobject)env->NewGlobalRef(javaJNI);
env->CallVoidMethod(javaJNI,javaPageMethod,wMethod);
}
Java代码:
public class OFActivity extends Activity {
.........
}
Java代码:
public class JNIPage extends Activity {
public static int wPage;
ImageView imageBatteryEmpty;
JNIPage(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
public void changePage (int a1) {
wPage = a1;
if (wPage == 0) {
setContentView(R.layout.battery);
imageBatteryEmpty = (ImageView) findViewById(R.id.imageView1);
imageBatteryEmpty.setImageResource(R.drawable.battery_low_42x);
}
}
}
battery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="28dp"
android:paddingTop="510dp"
android:src="@drawable/batteryempty2x" />
</LinearLayout>
logcat的
08-19 16:09:12.714: W/dalvikvm(12900): threadid=11: thread exiting with uncaught exception (group=0x40ae1480)
08-19 16:09:12.714: E/EmbeddedLogger(433): App crashed! Process: cc.openframeworks.androidEmptyExample
08-19 16:09:12.714: E/EmbeddedLogger(433): App crashed! Package: cc.openframeworks.androidEmptyExample v1 (1.0)
08-19 16:09:12.714: E/AndroidRuntime(12900): FATAL EXCEPTION: GLThread 1230
08-19 16:09:12.714: E/AndroidRuntime(12900): java.lang.NullPointerException
08-19 16:09:12.714: E/AndroidRuntime(12900): at android.app.Activity.setContentView(Activity.java:1897)
08-19 16:09:12.714: E/AndroidRuntime(12900): at cc.openframeworks.androidEmptyExample.JNIPage.changePage(JNIBattery.java:42)
08-19 16:09:12.714: E/AndroidRuntime(12900): at cc.openframeworks.OFAndroid.render(Native Method)
08-19 16:09:12.714: E/AndroidRuntime(12900): at cc.openframeworks.OFAndroidWindow.onDrawFrame(OFAndroid.java:841)
08-19 16:09:12.714: E/AndroidRuntime(12900): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1470)
08-19 16:09:12.714: E/AndroidRuntime(12900): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1224)
08-19 16:09:12.714: W/ActivityManager(433): Force finishing activity cc.openframeworks.androidEmptyExample/.OFActivity
答案 0 :(得分:0)
Android Activity
有相当多的内部管理维护。你的emtpy构造函数初始化没有,导致NPE。
Activity
必须以startActivity()
或startActivityForResult()
开头。只是在上面的JNI代码中创建一个实例对象是行不通的。