2017-02-13 4 views
0

私はAndroid開発の初心者で、hello-jniというMSDNのサンプルソリューションを試してみることにしました:Native/C++/Java Android NDK/JNI - アクティビティ間でネイティブコードを共有する(MSDN hello-jniサンプル変更)

https://code.msdn.microsoft.com/hello-jni-android-790ab73d

サンプルを変更せずに開き、Visual Studio 2015コミュニティから実行すると正常に動作します。私は私のすべてのテストのために私のLG L16Cの格安アンドロイド携帯電話(アンドロイド4.4.2、Android 19に対してコンパイル)で実行しています。

"R"識別子がIntellisenseに表示されないにもかかわらず、独自のレイアウトで問題なく、追加のアクティビティを作成できました。このサンプルにはJavaから呼び出されたネイティブコードメソッドがあり、これは文字列を返し、 "メインアクティビティ"のHelloJNI Javaクラスで利用されます。これもすべて問題なく動作します。

しかし、別のアクティビティクラスで同じコード(ネイティブコードを呼び出すネイティブC++とJavaコード)を使用しようとすると失敗します。 Visual StudioでAndroid Studioで試した各ステップを複製して比較目的でhello-jniサンプルを複製したところ、同じ結果が得られました - 2番目のアクティビティがネイティブコードを使用しようとすると、 。

私がここで間違っていると思われることは何ですか?

あなたはそのままサンプルをダウンロードして、次の変更を追加した場合、あなたは問題を再現することができます

AnotherActivity.java:

package com.miahcode.hellojni; 
 

 
import android.app.Activity; 
 
import android.content.Intent; 
 
import android.os.Bundle; 
 
import android.view.View; 
 
import android.widget.TextView; 
 

 
public class AnotherActivity extends Activity { 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     
 
\t \t TextView tv = new TextView(this); 
 
\t \t tv.setText(stringFromJNI()); 
 
     setContentView(tv); 
 
    } 
 

 
\t public native String stringFromJNI(); 
 

 
\t static { 
 
     System.loadLibrary("hello-jni"); 
 
    } 
 
}

AndriodManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 
     package="com.miahcode.hellojni" 
 
     android:versionCode="1" 
 
     android:versionName="1.0"> 
 
    <uses-sdk android:minSdkVersion="3" /> 
 
    <application android:label="@string/app_name" 
 
       android:debuggable="true"> 
 
    <activity android:name=".HelloJni" 
 
       android:label="@string/app_name"> 
 
     <intent-filter> 
 
     <action android:name="android.intent.action.MAIN" /> 
 
     <category android:name="android.intent.category.LAUNCHER" /> 
 
     </intent-filter> 
 
    </activity> 
 
    <activity android:name=".AnotherActivity"></activity> 
 
    </application> 
 
</manifest>
ここ

ネイティブコードを保有ハローjni.cは次のとおりです。

/* 
 
* Copyright (C) 2009 The Android Open Source Project 
 
* 
 
* Licensed under the Apache License, Version 2.0 (the "License"); 
 
* you may not use this file except in compliance with the License. 
 
* You may obtain a copy of the License at 
 
* 
 
*  http://www.apache.org/licenses/LICENSE-2.0 
 
* 
 
* Unless required by applicable law or agreed to in writing, software 
 
* distributed under the License is distributed on an "AS IS" BASIS, 
 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 
* See the License for the specific language governing permissions and 
 
* limitations under the License. 
 
* 
 
*/ 
 
#include <string.h> 
 
#include <jni.h> 
 

 
/* This is a trivial JNI miahcode where we use a native method 
 
* to return a new VM String. See the corresponding Java source 
 
* file located at: 
 
* 
 
* apps/samples/hello-jni/project/src/com/miahcode/hellojni/HelloJni.java 
 
*/ 
 
jstring Java_com_miahcode_hellojni_HelloJni_stringFromJNI(JNIEnv* env, jobject thiz) 
 
{ 
 
#if defined(__arm__) 
 
    #if defined(__ARM_ARCH_7A__) 
 
    #if defined(__ARM_NEON__) 
 
     #if defined(__ARM_PCS_VFP) 
 
     #define ABI "armeabi-v7a/NEON (hard-float)" 
 
     #else 
 
     #define ABI "armeabi-v7a/NEON" 
 
     #endif 
 
    #else 
 
     #if defined(__ARM_PCS_VFP) 
 
     #define ABI "armeabi-v7a (hard-float)" 
 
     #else 
 
     #define ABI "armeabi-v7a" 
 
     #endif 
 
    #endif 
 
    #else 
 
    #define ABI "armeabi" 
 
    #endif 
 
#elif defined(__i386__) 
 
    #define ABI "x86" 
 
#elif defined(__x86_64__) 
 
    #define ABI "x86_64" 
 
#elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */ 
 
    #define ABI "mips64" 
 
#elif defined(__mips__) 
 
    #define ABI "mips" 
 
#elif defined(__aarch64__) 
 
    #define ABI "arm64-v8a" 
 
#else 
 
    #define ABI "unknown" 
 
#endif 
 

 
    return (*env)->NewStringUTF(env, "Compiled with ABI " ABI "."); 
 
}

おかげで、 J

答えて

0

あなたのネイティブCコードを投稿する必要がありますまたはエラーログではわかりませんが、正しい関数名を持つように変更していないので、ネイティブ関数に一致するものは見つかりませんに。関数の名前Java_com_example_hellojni_HelloJni_stringFromJNIに注目してください。一致させるには、名前をJava_your_package_name_with_underscore_for_period_classname_functionnameにする必要があります。

+0

OK、ネイティブコードファイルを追加しました。私はあなたが正しいかもしれないと思います。私は "miahcode"にサンプルの部分を変更しました。私は、動作しているものと動作しないもので遊んでいました。私はネイティブコードで同じことをするが、他のアクティビティの命名規則を使って動作する新しい関数を試します。ご協力いただきありがとうございます。 – Miah

+0

ちょうどそれを試して、それは動作します! – Miah

関連する問題