Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9290145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:20:55+00:00 2026-06-18T20:20:55+00:00

I want to call a java method from native code. I followed the steps

  • 0

I want to call a java method from native code. I followed the steps given in:
Calling a java method from c++ in Android

However my application crashes at the place where the java function is called.

My java code is as shown below:

package com.example.jnitry;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("MYAPP","Ocreate entered....");
        Button btn=(Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                mainfunc(8);
            }
        });
    }

    public void display(byte[] byt){
        Log.d("MYAPP", "display() is entered....");
        for(int i=0;i<byt.length;i++)
            Log.d("MYAPP", "byt["+i+"]="+byt[i]);
        Log.d("MYAPP", "display finished");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public static native void mainfunc(int n);

    static
    {
        System.loadLibrary("mylib");
    }
}

My native code is as shown below:

#include <jni.h>
#include "com_example_jnitry_MainActivity.h"
#include <android/log.h>
#include <stdio.h>

JNIEXPORT void JNICALL Java_com_example_jnitry_MainActivity_mainfunc
  (JNIEnv *env, jclass obj, jint n){

    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","mainfunction entered...",NULL);
    int i;
    unsigned char arr[10];
    jbyteArray bArray=(*env)->NewByteArray(env,n);

    jclass cls = (*env)->FindClass(env, "com/example/jnitry/MainActivity");
        jmethodID mid = (*env)->GetMethodID(env, cls, "display", "([B)V");
        if (mid == 0){
            __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","mid==0",NULL);
            return;
        }
        if(bArray==NULL)
            {   __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","bArray==NULL...",NULL);
                return ;
            }
    for(i=0;i<n;i++){
        arr[i]=i;
        __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Iteration number:%d",i);
    }

    (*env)->SetByteArrayRegion(env,bArray,0,n,arr);
    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","bArray successfully created...",NULL);
    (*env)->CallVoidMethod(env, obj, mid, bArray);
    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Returned from disp() java function.....",NULL);

}

My app crashes after printing the log message:

02-14 15:37:06.814: D/MYAPP(11584): bArray successfully created...

Can anyone please tell me the reason. And provide a solution for the same. Thanks in advance.

Note: I have already tried CallVoidMedthodA(), callByteMethod(), CallObjectMethod() but they outcome was the same.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:20:56+00:00Added an answer on June 18, 2026 at 8:20 pm

    Your native method mainFunc is a static method – it does not have a valid this pointer. Meanwhile, display is an instance method – it needs one.

    The second parameter of mainFunc is actually a class object pointer for MainActivity, as per JNI rules. Declare mainFunc as nonstatic on Java side, and this will work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call a native c++ method from java (android) code, and pass
In my application I want to call service from Receiver. This is my NotificationReceiver.java
I want to call a method say success(message) of javascript from android activity. I
I am trying to build android application using native code so i want to
I have a Java method I want to call from JRuby. The argument I
I want to know how to call a java method from HTML (I Use
I want to call shell script on windows environment using java code. I am
I want to call a C subroutine from Java. I'm using JNI. I have
I want to call Java class methods from a cpp file that receives call
Possible Duplicates: I am looking forward to call some javascript method from a java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.