2014-10-12 100 views
0

我刚开始android应用程序开发。我有使用java开发应用程序的新手经验。在这里例如我写了一个简单的斐波那契程序,其中数字被添加和显示。我在java中编写了用于迭代和递归的编码,在c中用于迭代和递归。并比较它们的执行时间。该程序被安装在模拟器中,当我尝试计算该值时,它只是退出。在Logcat中,它显示以下异常和错误。谁能告诉我这些是如何解决它们的。我已经添加了下面的代码。错误:关于android中的NDK仿真

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) { 
       // TODO Auto-generated method stub 
       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; 
} 

下面是错误的名单,我得到 https://drive.google.com/file/d/0B3NEoejIh-a0S0dOcVlZOVFHU00/view?usp=sharing[ ^]

回答

0

看来,你的本机库(libcom_android_fibnative_FibLib .so)缺失。

它是否在你的APK? (lib/(x86|armeabi|armeabi-v7a)/下,你可以看看你的APK里面打开它作为一个.zip文件。)

+0

的libcom_android_fibnative_FibLib.so文件存在于所有架构armeabi,armeabi-V7A,MIPS和x86。 另一个编辑:我试着从谷歌运行示例NDK代码,它也在LogCat中返回错误。 – 2014-10-13 20:08:45

+0

您的FibLib类的包名和实现是否正确匹配?因为在你的C中,你的函数以'Java_com_android_fibnat'开头,所以它应该是'com.android.fibnat'。 /data/app-lib/com.android.fibnat-1/libcom_android_fibnative_FibLib.so是否存在?您可以轻松检查所有这些功能,并且使用我的应用程序正确导出功能:https://play.google.com/store/apps/details?id = com.xh.nativelibsmonitor.app&hl = en – ph0b 2014-10-14 08:13:55