当我使用jniSvmTrain时,应用程序崩溃

时间:2017-03-08 14:52:46

标签: android libsvm

当我运行jniSvmTrain(svmTrainOptions + dataTrainPath + modelPath);时,应用程序崩溃,我无法找出原因(删除此行时效果很好)。目的是构建一个基于AndroidLibSVM的应用程序,在iris数据集上训练它。我按照此处的说明在Android Studio中集成了AndroidLibSVM:https://github.com/yctung/AndroidLibSvm

我尝试在AndroidLibSVM上关注this tutorial

这是 IrisActivity.java 文件的代码:

package example.iris;

import android.support.v7.app.AppCompatActivity;
import android.os.Environment;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class IrisActivity extends AppCompatActivity {

    Button go;

    String appFolderPath;
    String systemPath;

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

    private native void jniSvmTrain(String cmd);
    private native void jniSvmPredict(String cmd);

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

        go = (Button)findViewById(R.id.go);

        systemPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
        appFolderPath = systemPath + "libsvm/";

        // iris.train is available here : https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/iris.scale
        final String dataTrainPath = appFolderPath + "iris.train ";
        final String dataPredictPath = appFolderPath + "iris.predict ";
        final String modelPath = appFolderPath + "iris.model ";
        final String outputPath = appFolderPath + "iris.output ";

        final String svmTrainOptions = "-s 0 -t 2 -g 1 -r 0 ";

        go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // The next line conducts the app to crash, but why ?
                jniSvmTrain(svmTrainOptions + dataTrainPath + modelPath);
            }
        });
    }
}

比你的帮助。

2 个答案:

答案 0 :(得分:1)

  

当我运行jniSvmTrain(svmTrainOptions + dataTrainPath + modelPath);时,   应用程序崩溃

您应该将 super.onCreate(savedInstanceState); setContentView(R.layout.activity_iris); go = (Button)findViewById(R.id.go); 放在{{1}}

之下
{{1}}

答案 1 :(得分:1)

感谢logcat,我发现了问题:

03-09 02:34:04.252 27921-27921/example.iris D/AndroidLibSvmNDK: NDK: jniSvmTrain cmd = -s 0 -t 2 -g 1 -r 0 /storage/emulated/0/libsvm/iris.train /storage/emulated/0/libsvm/iris.model
03-09 02:34:04.256 27921-27921/example.iris D/AndroidLibSvmNDK: NDK: can't open input file /storage/emulated/0/libsvm/iris.train

解决方案:应用崩溃是因为我忘记将<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />添加到 AndroidManifest.xml 文件中,以获取访问内部内存的权限。