Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all you are getting this error because you are not declaring parameter in is must to create conection between java and c/c++.</p> <p>SO, i am sending you my code for your problem </p> <p>1.First of all create android project in eclipse.</p> <ol start="2"> <li><p>create folder under project click -> click new -> then folder and name it jni.</p></li> <li><p>create one more folder under jni nameing include.</p></li> <li><p>create java class.</p></li> <li><p>code for java class nameing-(MainActivity.java)-></p> <pre><code> package com.example.ndk; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { static { System.loadLibrary("myjni"); } /** * Adds two integers, returning their sum */ public native int add( int v1, int v2 ); /** * Returns Hello World string */ public native String hello(); @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; } } </code></pre></li> <li><p>open command prompt or press window+R.</p></li> <li><p>move to directory- (workspace-> project name -> jni -> include).</p></li> <li><p>run command under this directory.</p> <pre><code> javah -classpath &lt;project-name&gt;/bin/classes;&lt;ANDROID_SDK_HOME&gt;\platforms\android-&lt;xx&gt;\android.jar -o HelloJNI.h com.example.test.MainActivity </code></pre></li> <li><p>after this we can see "HelloJNI.h" file under include folder.</p></li> <li><p>check "HelloJNI.h" have this lines in it </p> <pre><code>JNIEXPORT jint JNICALL Java_com_example_ndk_MainActivity_add(JNIEnv *, jobject, jint, jint); JNIEXPORT jstring JNICALL Java_com_example_ndk_MainActivity_hello (JNIEnv *, jobject); </code></pre></li> <li><p>create new file under jni naming test.c (use this 2 points in pont 10 in this file test.c)</p> <pre><code> #include &lt;jni.h&gt; #include "include/HelloJNI.h" JNIEXPORT jstring JNICALL Java_com_example_ndk_MainActivity_hello (JNIEnv *env, jobject javaThis) { return (*env)-&gt;NewStringUTF(env, "Hello"); } JNIEXPORT jint JNICALL Java_com_example_ndk_MainActivity_add (JNIEnv *env, jobject javaThis, jint value1, jint value2){ return (value1 + value2); } </code></pre></li> <li><p>create new file under jni naming Android.mk</p> <pre><code>LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := myjni // from point 5 LOCAL_SRC_FILES := test.c //from point 10 that we creare test.c include $(BUILD_SHARED_LIBRARY) </code></pre></li> <li><p>create new file NDKActivity.java</p> <pre><code> package com.example.ndk; import android.app.Activity; import android.view.View.OnClickListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class NDKActivity extends Activity{ Button buttonCalc; TextView result; EditText value1,value2; /** Called when the activity is first created. */ MainActivity nativeLib; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nativeLib = new MainActivity(); String helloText = nativeLib.hello(); result = (TextView) findViewById(R.id.result); value1 = (EditText) findViewById(R.id.value1); value2 = (EditText) findViewById(R.id.value2); // Update the UI TextView outText = (TextView) findViewById(R.id.textOut); outText.setText(helloText); // Setup the UI buttonCalc = (Button)this.findViewById(R.id.buttonCalc); buttonCalc.setOnClickListener(new OnClickListener() { public void onClick(View v) { int v1, v2, res = -1; v1 = Integer.parseInt(value1.getText().toString().trim()); v2 = Integer.parseInt(value2.getText().toString().trim()); res = nativeLib.add(v1, v2); result.setText(new Integer(res).toString()); } }); } } </code></pre></li> <li><p>run ndk-build in command promt</p></li> </ol> <p>go to project directory-> then, write this command <code>&lt;android-ndk-directory&gt;\ndk-build.cmd</code> and hit enter</p> <p>after this we can check .so file under obj folder</p> <ol start="14"> <li><p>xml file for NDKActivity.</p> <p> </p> <pre><code> &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Vikram" android:textSize="22sp"/&gt; &lt;TextView android:id="@+id/textOut" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Output"/&gt; &lt;EditText android:id="@+id/value1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Value 1" android:inputType="numberDecimal" /&gt; &lt;TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="+" android:textSize="36sp" /&gt; &lt;EditText android:id="@+id/value2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Value 2" android:inputType="numberDecimal" /&gt; &lt;Button android:id="@+id/buttonCalc" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="=" /&gt; &lt;TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="result" android:textSize="36sp" /&gt; &lt;/LinearLayout&gt; </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload