我已经使用了许多教程来开始使用android和jni。我的上一次是this step by step tutorial。我经历了所有的步骤。但是当我运行它时,应用程序停止运行。我用过genymotion
在jni文件夹中创建Android.mk文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello
LOCAL_SRC_FILES := hello.cpp
include $(BUILD_SHARED_LIBRARY)
在jni文件夹文件中创建hello.cpp
#include <jni.h>
jint Java_com_nikolay_hellotest_MainActivity(JNIEnv* env, jobject javaThis, jint a) {
return a*a;
}
打开终端并转到我的项目文件夹,然后成功运行ndk-build:
[armeabi] Compile++ thumb: hello <= hello.cpp
[armeabi] StaticLibrary : libstdc++.a
[armeabi] SharedLibrary : libhello.so
[armeabi] Install : libhello.so => libs/armeabi/libhello.so
向MainActivity添加一些代码:
package com.nikolay.hellotest;
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 {
// load the library - name matches jni/Android.mk
static {
System.loadLibrary("hello");
}
// declare the native code function - must match hello.cpp
public native int getIntValue(int val);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView tv = (TextView) rootView.findViewById(R.id.textView1);
tv.setText(Integer.toString(new MainActivity().getIntValue(24)));
return rootView;
}
}
}
添加了:
static {
System.loadLibrary("hello");
}
// declare the native code function - must match hello.cpp
public native int getIntValue(int val);
和
TextView tv = (TextView) rootView.findViewById(R.id.textView1);
tv.setText(Integer.toString(new MainActivity().getIntValue(24)));
我无法理解错误。感谢任何帮助。
答案 0 :(得分:2)
修改|在jni文件夹“APP_ABI:= x86”
中添加Application.mk