title: NDK: Pass text field value to c++ string
body: <p>Im into android but not the apps itself. Im trying to re-create my school work as an Android app with NDK -> <a href="https://github.com/MyczkowskiAdam/LuckyNumbersCppCMD/blob/visual-studio/LuckyNumbers.cpp" rel="nofollow noreferrer">MyczkowskiAdam/LuckyNumbersCppCMD</a>
  Printing output from C++ to text field is not a problem but in order for my program to work I have to pass two strings (FirstName &amp; LastName) with input from textfield to C++ and store them so that main program can interpret it and thats what Im really struggling with.</p>
  
  <p>In my C++ file I have the main program process to which I have to pass FrNm and LstNm:</p>
  
  <pre><code>int LuckyNumbersProcess(std::string FrNm, std::string LstNm) {
      //stuff
      //return LuckyNumber (int)
  }
  </code></pre>
  
  <p>Then I have the native function that runs LuckyNumbersProcess and writes LuckyNumber to a textfield:</p>
  
  <pre><code>extern "C"
  JNIEXPORT jstring JNICALL 
  Java_com_android_mycax_luckynumbers_MainActivity_Calculate (JNIEnv *env, 
  jobject ) {
      LuckyNumbersProcess(FrNmFromJava, LstNmFromJava);
      std::string sLuckyNumber = NumberToString(LuckyNumber);
      return env-&gt;NewStringUTF(sLuckyNumber.c_str());
  }
  </code></pre>
  
  <p>Input from text fields show be stored in FrNmFromJava and LstNmFromJava but how do I go about this?</p>
  
  <p>I have tried this method:</p>
  
  <pre><code>extern "C"
  JNIEXPORT void JNICALL 
  Java_com_android_mycax_luckynumbers_MainActivity_GetFirstName (JNIEnv * env, 
  jobject jobj) {
  
      const char *str;
  
      jclass myclass_class =(jclass) env-&gt;NewGlobalRef
              (env-&gt;FindClass ("GetFirstNameFromField"));
  
      // we need the MyClass constructor
      jmethodID constructorID = env-&gt;GetMethodID
              (myclass_class, "", "()V");
  
      // and the sayHello() method
      jmethodID methodID = env-&gt;GetMethodID
              (myclass_class, "iFirstName", "()Ljava/lang/String;");
  
      // instanciate a MyClass object
      jobject myclass_object =  env-&gt;NewObject
              (myclass_class, constructorID);
  
      // call the sayHello() method
      jstring s = (jstring)  env-&gt;CallObjectMethod
              (myclass_object, methodID);
  
      // convert the Java String to use it in C
      str = env-&gt;GetStringUTFChars(s, 0);
      //printf("%s" , str);
      FrNmFromJava = str;
      env-&gt;ReleaseStringUTFChars(s, str);
  
  }
  </code></pre>
  
  <p>But it returns that JNI NewGlobalRef called with pending exception java.lang.ClassNotFoundException: Didn't find class "GetFirstNameFromField" on path:</p>
  
  <p>This is my Java file:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  
  // Used to load the 'native-lib' library on application startup.
  static {
      System.loadLibrary("luckynumbers");
  }
  
  EditText mFirstName;
  EditText mLastName;
  Button mButton;
  GetFirstNameFromField gfn = new GetFirstNameFromField();
  GetLastNameFromField gln = new GetLastNameFromField();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      mFirstName = (EditText)findViewById(R.id.firstName);
      mLastName = (EditText)findViewById(R.id.lastName);
      mButton = (Button)findViewById(R.id.button);
      mButton.setOnClickListener(
              new View.OnClickListener()
              {
                  public void onClick(View view)
                  {
                      gfn.iFirstName();
                      gln.iLastName();
                      GetFirstName();
                      GetLastName();
                      TextView tv = (TextView) findViewById(R.id.sample_text);
                      tv.setText(Calculate());
                  }
              });
  }
  
  public class GetFirstNameFromField {
      public String iFirstName() {
          return mFirstName.getText().toString();
      }
  }
  public class GetLastNameFromField {
      public String iLastName() {
          return mLastName.getText().toString();
      }
  }
  /**
   * A native method that is implemented by the 'native-lib' native library,
   * which is packaged with this application.
   */
  public native String Calculate();
  
  public native String GetFirstName();
  
  public native String GetLastName();
  }
  </code></pre>
  
  <p>Any help would be appreciated :)</p>