好的,所以...已经到了最后。
尝试学习使用本机开发工具包...从C语言源返回字符串“Hello World”时,从Hello World开始。
开始:这是“主要活动”。
package com.example.hellojni;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView=(TextView) findViewById(R.id.text1);
textView.setText(sayHelloWorld());
}
private native String sayHelloWorld();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
static {
System.loadLibrary("HelloJni");
}
}
为了完整起见,这里是定义显示的XML。我敢向甜甜圈打赌这不是问题的所在,但如果我没有提及它,我会在学术上疏忽。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
接下来,我们当然需要由javah生成的头文件。
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_hellojni_MainActivity */
#ifndef _Included_com_example_hellojni_MainActivity
#define _Included_com_example_hellojni_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
#undef com_example_hellojni_MainActivity_MODE_PRIVATE
#define com_example_hellojni_MainActivity_MODE_PRIVATE 0L
#undef com_example_hellojni_MainActivity_MODE_WORLD_READABLE
#define com_example_hellojni_MainActivity_MODE_WORLD_READABLE 1L
#undef com_example_hellojni_MainActivity_MODE_WORLD_WRITEABLE
#define com_example_hellojni_MainActivity_MODE_WORLD_WRITEABLE 2L
#undef com_example_hellojni_MainActivity_MODE_APPEND
#define com_example_hellojni_MainActivity_MODE_APPEND 32768L
#undef com_example_hellojni_MainActivity_MODE_MULTI_PROCESS
#define com_example_hellojni_MainActivity_MODE_MULTI_PROCESS 4L
#undef com_example_hellojni_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING
#define com_example_hellojni_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L
#undef com_example_hellojni_MainActivity_BIND_AUTO_CREATE
#define com_example_hellojni_MainActivity_BIND_AUTO_CREATE 1L
#undef com_example_hellojni_MainActivity_BIND_DEBUG_UNBIND
#define com_example_hellojni_MainActivity_BIND_DEBUG_UNBIND 2L
#undef com_example_hellojni_MainActivity_BIND_NOT_FOREGROUND
#define com_example_hellojni_MainActivity_BIND_NOT_FOREGROUND 4L
#undef com_example_hellojni_MainActivity_BIND_ABOVE_CLIENT
#define com_example_hellojni_MainActivity_BIND_ABOVE_CLIENT 8L
#undef com_example_hellojni_MainActivity_BIND_ALLOW_OOM_MANAGEMENT
#define com_example_hellojni_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 16L
#undef com_example_hellojni_MainActivity_BIND_WAIVE_PRIORITY
#define com_example_hellojni_MainActivity_BIND_WAIVE_PRIORITY 32L
#undef com_example_hellojni_MainActivity_BIND_IMPORTANT
#define com_example_hellojni_MainActivity_BIND_IMPORTANT 64L
#undef com_example_hellojni_MainActivity_BIND_ADJUST_WITH_ACTIVITY
#define com_example_hellojni_MainActivity_BIND_ADJUST_WITH_ACTIVITY 128L
#undef com_example_hellojni_MainActivity_CONTEXT_INCLUDE_CODE
#define com_example_hellojni_MainActivity_CONTEXT_INCLUDE_CODE 1L
#undef com_example_hellojni_MainActivity_CONTEXT_IGNORE_SECURITY
#define com_example_hellojni_MainActivity_CONTEXT_IGNORE_SECURITY 2L
#undef com_example_hellojni_MainActivity_CONTEXT_RESTRICTED
#define com_example_hellojni_MainActivity_CONTEXT_RESTRICTED 4L
#undef com_example_hellojni_MainActivity_RESULT_CANCELED
#define com_example_hellojni_MainActivity_RESULT_CANCELED 0L
#undef com_example_hellojni_MainActivity_RESULT_OK
#define com_example_hellojni_MainActivity_RESULT_OK -1L
#undef com_example_hellojni_MainActivity_RESULT_FIRST_USER
#define com_example_hellojni_MainActivity_RESULT_FIRST_USER 1L
#undef com_example_hellojni_MainActivity_DEFAULT_KEYS_DISABLE
#define com_example_hellojni_MainActivity_DEFAULT_KEYS_DISABLE 0L
#undef com_example_hellojni_MainActivity_DEFAULT_KEYS_DIALER
#define com_example_hellojni_MainActivity_DEFAULT_KEYS_DIALER 1L
#undef com_example_hellojni_MainActivity_DEFAULT_KEYS_SHORTCUT
#define com_example_hellojni_MainActivity_DEFAULT_KEYS_SHORTCUT 2L
#undef com_example_hellojni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL
#define com_example_hellojni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
#undef com_example_hellojni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL
#define com_example_hellojni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
/*
* Class: com_example_hellojni_MainActivity
* Method: sayHelloWorld
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_hellojni_MainActivity_sayHelloWorld (JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
下一个要看的自然事物是C源......
#include "com_example_hellojni_MainActivity.h"
jstring Java_com_example_HelloWorldActivity_sayHelloWorld(JNIEnv* pEnv, jobject thiz) {
return (pEnv->NewStringUTF("Hello World from C!"));
//return (*pEnv)->NewStringUTF(env,"Hello World from C!");
}
评论版,我不记得我从哪里得到的...很可能被转录为书籍。无论如何,它甚至都没有编译。我使用的版本对我来说更有意义。也许这就是问题所在。
接着,这是android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := HelloJni
LOCAL_SRC_FILES := HelloJni.cpp
include $(BUILD_SHARED_LIBRARY)
当然......当我把所有这些放在一起时,我得到一个编译并尝试运行的apk。我已经把头发拉了好几天的笑话的妙语如下:
03-22 21:34:38.218: I/ActivityManager(322): Start proc com.example.hellojni for activity com.example.hellojni/.MainActivity: pid=20945 uid=10175 gids={}
03-22 21:34:38.278: D/dalvikvm(20945): Trying to load lib /data/data/com.example.hellojni/lib/libHelloJni.so 0x417260f0
03-22 21:34:38.278: D/dalvikvm(20945): Added shared lib /data/data/com.example.hellojni/lib/libHelloJni.so 0x417260f0
03-22 21:34:38.278: D/dalvikvm(20945): No JNI_OnLoad found in /data/data/com.example.hellojni/lib/libHelloJni.so 0x417260f0, skipping init
03-22 21:34:38.318: W/dalvikvm(20945): No implementation found for native Lcom/example/hellojni/MainActivity;.sayHelloWorld ()Ljava/lang/String;
03-22 21:34:38.328: E/AndroidRuntime(20945): at com.example.hellojni.MainActivity.sayHelloWorld(Native Method)
03-22 21:34:38.328: E/AndroidRuntime(20945): at com.example.hellojni.MainActivity.onCreate(MainActivity.java:17)
03-22 21:34:38.328: W/ActivityManager(322): Force finishing activity com.example.hellojni/.MainActivity
03-22 21:34:38.868: W/ActivityManager(322): Activity pause timeout for ActivityRecord{4235a518 com.example.hellojni/.MainActivity}
03-22 21:34:49.008: W/ActivityManager(322): Activity destroy timeout for ActivityRecord{4235a518 com.example.hellojni/.MainActivity}
03-22 21:34:56.208: I/ActivityManager(322): Process com.example.hellojni (pid 20945) has died.
请帮忙。沮丧是巨大的,理智正在消退。
答案 0 :(得分:3)
Praful Bhatnagar的评论被发现了。我修正了盯着我的错误,其他一切都陷入了困境。谢谢!
(不确定我是否应该做些什么才能正确地将答案归功于说出来的人)