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
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) {
dialog.dismiss();
FibActivity.this.output.setText(result);
}
}.execute();
}
}
com_android_fibnat_FibLib.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[
^]