how to crate the object of c++ class in android in ndk

Tutorials with advanced 'difficulty' and more Lines of Code.

how to crate the object of c++ class in android in ndk

Postby Umakant » Mon Nov 23, 2009 7:11 am

my activity is::

package com.soft;

import android.os.Bundle;
import android.widget.TextView;
public class TestNdk extends Activity {
TextView txtHello;

private int m_cAddValue;
public native void MyClass(int pNum1 ,int pNum2);
public native int getSum();

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

//obtain a reference to our textview control
txtHello = (TextView) findViewById(;
txtHello.setText("hai this is for test");
//public TestNdk(int i, int j) ;
//TestNdk m_cObj = new TestNdk(20 ,30);
//invoke our native method
MyClass(20 ,30);
int lAddValue = getSum();

//update our textview control


//Our native method. This is needed by the "javah" tool
//for generating our .h include header.

//Our static initializer which will load our shared library
//into our classloader/VM


my header test.h

#ifndef __TEST_H__
#define __TEST_H__

class MyClass
int m_cFirstNum;
int m_cSecondNum;
MyClass(int pNum1, int pNum2);
int getSum();
int getMult();
int getSub();
int getDiv();

#endif //__TEST_H__

my cpp class is test.cpp

#include "test.h"

MyClass::MyClass(int pNum1, int pNum2)
m_cFirstNum = pNum1;
m_cSecondNum = pNum2;

int MyClass::getSum()
return m_cFirstNum + m_cSecondNum;

int MyClass::getMult()
return m_cFirstNum * m_cSecondNum;

int MyClass::getSub()
return m_cFirstNum - m_cSecondNum;

int MyClass::getDiv()
int lRetVal = 0;
if(0 != m_cSecondNum)
lRetVal = m_cFirstNum + m_cSecondNum;
return lRetVal;


can any body help me in this
i am unable to call my c++ methods

thanks in advance
Posts: 5
Joined: Fri Nov 06, 2009 5:31 am


Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests