问题描述
- Android之ndk开发,使用ant自动创建头文件 出错
-
这是ant源码
<?xml version="1.0" encoding="UTF-8"?>description
<!-- ================================= target: BuildAllHeaders ================================= --> <target name="BuildAllHeaders" > <antcall target="BuildGetStringHeader"></antcall> <antcall target="BuildGetIntHeader"></antcall> </target> <!-- - - - - - - - - - - - - - - - - - target: depends - - - - - - - - - - - - - - - - - --> <target name="BuildGetStringHeader"> <javah destdir="./jni" classpath="./bin/classes/" class="com.longertek.hellondk.GetString"></javah> </target> <!-- - - - - - - - - - - - - - - - - - target: BuildGetIntHeader - - - - - - - - - - - - - - - - - --> <target name="BuildGetIntHeader"> </target>
双击ant出现如下错误
Buildfile: E:workspaceAndroidHelloNDKbuilder_headers.xml
BuildAllHeaders:
BuildGetStringHeader:
[javah] Exception in thread "main" java.lang.NullPointerException
[javah] at com.sun.tools.javah.JavahTask.run(JavahTask.java:503)
[javah] at com.sun.tools.javah.JavahTask.run(JavahTask.java:329)
[javah] at com.sun.tools.javah.Main.main(Main.java:46)BUILD FAILED
E:workspaceAndroidHelloNDKbuilder_headers.xml:19: The following error occurred while executing this line:
E:workspaceAndroidHelloNDKbuilder_headers.xml:27: compilation failedTotal time: 880 milliseconds
这个错误是什么原因呢
解决方案
提示编译错误,你的页面上的代码有问题。
解决方案二:
package com.longertek.hellondk;
public class GetString {
public static native String getStr();
}
这个是我的GetString类的代码
package com.longertek.hellondk;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity {
static{
System.loadLibrary("HelloNDK");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) super.findViewById(R.id.tv);
text.setText(GetString.getStr());
}
}
这是main代码,