出于某种原因,我只能从我的主要活动中调用本机函数,而不能从我创建的任何自定义视图中调用。这是一个示例文件(我遵循了一个教程,但将类重命名为http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/)
请参阅本机函数“getNewString”的用法。
package com.example.native;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
public class NativeTestActivity extends Activity
{
static
{
System.loadLibrary("nativeTest");
}
private native String getNewString();
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(new BitmapView(this));
String hello = getNewString(); // This line works fine
new AlertDialog.Builder(this).setMessage(hello).show();
}
}
class BitmapView extends View
{
static
{
System.loadLibrary("nativeTest");
}
private native String getNewString();
public BitmapView(Context context)
{
super(context);
String hello = getNewString(); // This line throws the UnsatisfiedLinkError
new AlertDialog.Builder(this.getContext()).setMessage(hello).show();
}
}
如何在自定义视图中调用本机函数?
我已将该应用程序构建为 Android 2.2 应用程序。我正在我的 HTC Desire 上运行该应用程序。我有最新的 SDK (9) 和最新的 NDK (r5)。