Okay, so... have arrived at whits end.
好吧,所以…已经到达了whits末端。
Attempting to learn to utilize the native development kit... starting off with Hello World when the very string "Hello World" is returned from a C source.
试图学习使用本机开发工具包…从Hello World开始,当字符串“Hello World”从C源返回时。
To begin: Here's the "main activity".
开始:这里是“主要活动”。
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");
}
}
For completeness sake, here's the XML defining the display. I'll bet dollars to doughnuts that this is not where the problem is, but I'd be academically negligent if I didn't mention it.
为了完整起见,这里是定义显示的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>
Following that, we of course need the header file which was generated by javah.
接下来,我们当然需要由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
The next natural thing to look at would be the C source....
下一个自然的事情看是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!");
}
The commented version, I can't remember where I got that from... likely was transcribed as out of a book. In any case, it didn't even compile. The version I used makes much more sense to me. Maybe this is the problem.
注释版,我不记得我是从哪里得到的…很可能是从一本书中抄写出来的。无论如何,它甚至没有编译。我使用的版本对我来说更有意义。也许这就是问题所在。
Following along, here's android.mk:
后,android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := HelloJni
LOCAL_SRC_FILES := HelloJni.cpp
include $(BUILD_SHARED_LIBRARY)
And of course... when I put all this together, I get an apk that compiles and attempts to run. The punchline to the joke that I've been pulling my hair out for several days now is below:
当然……当我把所有这些放在一起时,我得到一个编译和尝试运行的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.
Please help. Frustration is immense, sanity is fading.
请帮助。沮丧是巨大的,理智正在衰退。
1 个解决方案
#1
2
Praful Bhatnagar's comment was spot on. I fixed the error that was staring me in the face and everything else fell in to plaece. Thanks!
Praful Bhatnagar发表评论。我修正了盯着我的脸的错误,其他的东西都掉在了上面。谢谢!
(not sure if there's something I'm supposed to do to correctly attribute the answer to the person who said it)
(不确定是否有我应该做的事情来正确地将答案归结于那个说它的人)
#1
2
Praful Bhatnagar's comment was spot on. I fixed the error that was staring me in the face and everything else fell in to plaece. Thanks!
Praful Bhatnagar发表评论。我修正了盯着我的脸的错误,其他的东西都掉在了上面。谢谢!
(not sure if there's something I'm supposed to do to correctly attribute the answer to the person who said it)
(不确定是否有我应该做的事情来正确地将答案归结于那个说它的人)