#android #android-ndk
#Android #android-ndk
Вопрос:
Получение PackageManager $ NameNotFoundException для кода захвата подписи в Android studio
package com.example.u29692.helloandroidjni;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((TextView) findViewById(R.id.jni_msgView)).setText(stringFromJNI(this));
}
// new code
static {
System.loadLibrary("hello-android-jni");
}
// public native String getMsgFromJni();
public native String stringFromJNI(Context context);
jni.c
#include <jni.h>
JNIEXPORT jstring JNICALL
Java_com_example_u29692_helloandroidjni_MainActivity_stringFromJNI(JNIEnv *env, jobject instance,
jobject context) {
// TODO
// For class Context
jclass native_clazz = (*env)->GetObjectClass(env, context);
// Get the getPackageManager method ID
jmethodID methodID_func = (*env)->GetMethodID(env, native_clazz,
"getPackageManager", "()Landroid/content/pm/PackageManager;");
// Access manager application package
jobject package_manager = (*env)->CallObjectMethod(env, context, methodID_func);
// For class PackageManager
jclass pm_clazz = (*env)->GetObjectClass(env, package_manager);
// Get the getPackageInfo method ID
jmethodID methodID_pm = (*env)->GetMethodID(env, pm_clazz,
"getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
// To obtain the application package information
jobject package_info = (*env)->CallObjectMethod(env, package_manager,
methodID_pm, (*env)->NewStringUTF(env, "com.example.u29692.helloandroidjni"), 64);
return (*env)->NewStringUTF(env, package_info);
}
Происходит сбой приложения с журналами:
10-14 12:12:55.038: A/art(17135): sart/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: JNI NewStringUTF called with pending exception 'android.content.pm.PackageManager$NameNotFoundException' thrown in unknown throw location
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
10-14 12:12:55.198: A/art(17135): sart/runtime/runtime.cc:292] Pending exception android.content.pm.PackageManager$NameNotFoundException thrown by 'unknown throw location'
0-14 12:12:55.198: A/art(17135): sart/runtime/runtime.cc:292] android.content.pm.PackageManager$NameNotFoundException: com.example.u29692.helloandroidjni
10-14 12:12:55.198: A/art(17135): sart/runtime/runtime.cc:292] at android.content.pm.PackageInfo android.app.ApplicationPackageManager.getPackageInfo(java.lang.String, int) (ApplicationPackageManager.java:167)
10-14 12:12:55.198: A/art(17135): sart/runtime/runtime.cc:292] at java.lang.String
Комментарии:
1. Это
com.example.u29692.helloandroidjni
имя пакета, которое вы указали в своем манифесте?2. да, файл манифеста: <manifest xmlns:android=» schemas.android.com/apk/res/android » пакет=»com.example.u29692.helloandroidjni»>