谁能告诉我如何通过ndk在android中创建c ++类的对象

时间:2009-11-23 11:12:48

标签: c++ android

我的活动是::

package com.soft;

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


    private int m_cAddValue;


    private TestNdk m_cTestNDK;


    private int m_cObj;
    public TestNdk(int i, int j) {
         getSum();
    }

    public native int getSum();

        /** Called when the activity is first created. */
         @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
                 setContentView(R.layout.main);



              txtHello = (TextView) findViewById(R.id.txtHello);
              txtHello.setText("hai this is for test");  
              //public TestNdk(int i, int j) ;

              m_cTestNDK = new TestNdk(20 ,30);





              txtHello.setText(m_cObj);

          }

         static{
             System.loadLibrary("TestNDK");
            initIDs();
             }


      }

=============================================== ==============

我的头文件是:: test.h

#ifndef __TEST_H__
#define __TEST_H__

class MyClass
{
private:
  int   m_cFirstNum;
  int   m_cSecondNum;
public:
  MyClass(int pNum1, int pNum2);
  int getSum();
  int getMult();
  int getSub();
  int getDiv();
};

#endif  //__TEST_H__

=============================================

我的cpp文件是:: 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;
}

请指导我 我总是得到这样的信息 :: newInstance失败:no()

1 个答案:

答案 0 :(得分:1)

你真的需要做两件事:

  1. 了解JNI:http://java.sun.com/docs/books/jni/html/jniTOC.html

  2. 了解Android特定位:http://developer.android.com/sdk/ndk/1.6_r1/index.html#samples

  3. 你所拥有的内容与JNI的使用方式相差甚远,所以你应该从头开始。 (这可能是最容易在桌面上学习的,因为网上有更多的文档和示例,甚至还有关于这个主题的书籍。桌面Java JNI和Android JNI之间的区别主要在于如何构建代码。)