Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello dear CPs,

I just started out android application development. I have a novice experience in developing applications using java. Here for example I wrote a simple Fibonacci program where the numbers are added and displayed. I wrote the coding in java for iteration and recursion, in c for iteration and recursion. And compare their execution time. The program gets installed in emulator and when I try to calculate the value, it just exits. In Logcat it shows the following exceptions and errors thrown. Can anyone tell me what these are how to solve them. I've added the coding below.

FibActivity.java

Java
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class FibActivity extends Activity implements OnClickListener {
	
	private EditText input;
	private RadioGroup type;
	private TextView output;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.input = (EditText) super.findViewById(R.id.input);
		this.type = (RadioGroup) super.findViewById(R.id.type);
		this.output = (TextView) super.findViewById(R.id.output);
		Button button = (Button) super.findViewById(R.id.button);
		button.setOnClickListener(this);
	}
	
	public void onClick(View view) {
		String s = this.input.getText().toString();
		if(TextUtils.isEmpty(s)) {
			return;
		}
		
		final ProgressDialog dialog = ProgressDialog.show(this, "", "Calculating...", true);
		final long n = Long.parseLong(s);
		new AsyncTask<Void, Void, String>() {

			@Override
			protected String doInBackground(Void... params) {
				long result = 0;
				long t = System.currentTimeMillis();
				switch(FibActivity.this.type.getCheckedRadioButtonId()) {
					case R.id.type_fib_jr:
						result = FibLib.fibJR(n);
					case R.id.type_fib_ji:
						result = FibLib.fibJI(n);
					case R.id.type_fib_nr:
						result = FibLib.fibNR(n);
					case R.id.type_fib_ni:
						result = FibLib.fibNI(n);
				}
				t = System.currentTimeMillis() - t;
				return String.format("fib(%d)=%d in %d ms", n, result, t);
			}
			
		
			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				FibActivity.this.output.setText(result);
			}
		
		}.execute();
		
	}
}


com_android_fibnat_FibLib.c

C++
#include <com_android_fibnat_FibLib.h>

static jlong fib(jlong n) {
	return n <= 0? 0 : n==1 ? 1 : fib(n-1) + fib(n-2);
}

JNIEXPORT jlong JNICALL Java_com_android_fibnat_FibLib_fibNR
  (JNIEnv *env, jclass c, jlong n) {
	return fib(n);
}

JNIEXPORT jlong JNICALL Java_com_android_fibnat_FibLib_fibNI
  (JNIEnv *env, jclass c, jlong n) {

	jlong p = -1;
	jlong result = 1;
	jlong i;
	for(i=0;i<=n;i++) {
		jlong sum = result + p;
		p = result;
		result = sum;
	}
	return result;
}



Here are the list of errors I get
https://drive.google.com/file/d/0B3NEoejIh-a0S0dOcVlZOVFHU00/view?usp=sharing[^]
Posted
Comments
Xiao Ling 12-Oct-14 20:13pm    
It's UnsatisfiedLinkError which means there's something wrong with your *.so. Could you post the Android.mk and FibLib.java?
Ashwin2013 12-Oct-14 22:18pm    
FibLib.java
<pre lang="java">
package com.android.fibnat;

import android.util.Log;

public class FibLib {

private static final String TAG = "FibLib";

public static long fib(long n) {
return n <= 0? 0 : n==1 ? 1 : fibJR(n-1) + fibJR(n-2);
}

public static long fibJR(long n) {
Log.d(TAG, "fibJR(" + n + ")");
return fib(n);
}

public native static long fibNR(long n);


public static long fibJI(long n) {
Log.d(TAG, "fibJI(" + n + ")");
long p = -1;
long result = 1;
long sum;
for(long i=0;i<=n;i++) {
sum = result + p;
p = result;
result = sum;
}
return result;
}

public native static long fibNI(long n);

static {
System.loadLibrary("com_android_fibnative_FibLib");
}
}
</pre>
Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := com_android_fibnat_FibLib.c
LOCAL_MODULE := com_android_fibnat_FibLib
include $(BUILD_SHARED_LIBRARY)
Ashwin2013 12-Oct-14 22:21pm    
Im running it on kitkat 4.4.2 API 19. I compiled FibLib using javah.exe and created .so files using ndk-build.exe

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900