系统环境:Android Studio 3.4,带有 Gradle 插件到 3.4.0 版和 Gradle 到 5.1.1 版。我创建了一个新项目 --> 文件 --> 新建 --> 导入示例 --> Ndk --> Hello JNI。我将 build.gradle 文件中的 minSdkVersion 更改为 22,因为我的 Nexus 7 不能做更多。然后我从https://sourceforge.net/projects/jexcelandroid/下载了 jexcel_android.jar 并将 jexcel_android.jar 复制到 ...app/libs/ 在 HelloJni.java 我添加了一些代码查看详细信息。在 AndroidManifest 中,我刚刚添加了写入 sdcard 的权限。
– 我没有更改或添加任何其他内容。我能够在 Nexus 7 和 Nexus 5x 上安装和运行该应用程序。Nexus 7 上创建的文件显示了预期的英文字母。创建的文件 om Nexus 5x 显示非预期的中文字母。为什么?通常,这两款设备在所有其他应用程序上都显示英文字母。
这里是我在 HelloJni.java 中添加的代码: String str ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Retrieve our TextView and set its content.
* the text is retrieved by calling a native
* function.
*/
setContentView(R.layout.activity_hello_jni);
TextView tv = (TextView)findViewById(R.id.hello_textview);
WritableWorkbook wb = createWorkbook("my.xls");
WritableSheet sh = createSheet(wb,"mysheet", 0);
try {
//writeCell(0, 0, "hi", true, sh);
wb.write();
wb.close();
}
catch (
IOException ex) {
ex.printStackTrace();
}
catch (
WriteException ex) {
ex.printStackTrace();
}
str += "\n" +stringFromJNI();
tv.setText( str );
}
/**
*
* @param wb - WritableWorkbook to create new sheet in
* @param sheetName - name to be given to new sheet
* @param sheetIndex - position in sheet tabs at bottom of workbook
* @return - a new WritableSheet in given WritableWorkbook
*/
public WritableSheet createSheet(WritableWorkbook wb,
String sheetName, int sheetIndex){
//create a new WritableSheet and return it
return wb.createSheet(sheetName, sheetIndex);
}
/**
*
* @param fileName - the name to give the new workbook file
* @return - a new WritableWorkbook with the given fileName
*/
public WritableWorkbook createWorkbook(String fileName){
//exports must use a temp file while writing to avoid memory hogging
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setUseTemporaryFileDuringWrite(true);
str += wbSettings.getLocale().getLanguage();
//Log.i("HelloJni", " wbSettings.getLocale().getLanguage()=" +wbSettings.getLocale().getLanguage());
//get the sdcard's directory
File sdCard = Environment.getExternalStorageDirectory();
//add on the your app's path
File dir = new File(sdCard.getAbsolutePath() + "/");
//make them in case they're not there
dir.mkdirs();
//create a standard java.io.File object for the Workbook to use
File wbfile = new File(dir,fileName);
WritableWorkbook wb = null;
try{
//create a new WritableWorkbook using the java.io.File and
//WorkbookSettings from above
wb = Workbook.createWorkbook(wbfile,wbSettings);
}catch(IOException ex){
Log.e(className,ex.getStackTrace().toString());
Log.e(className, ex.getMessage());
}
return wb;
}
Nexus 7 的 Android Studio 的 Logcat 看起来像预期的那样。但在 Nexus 5x 中,我得到了很多信息/错误输出。大约。5次这个东西,每次都一样,所以你只看到不重复的东西:
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:515)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:423)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:374)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void com.example.hellojni.HelloJni.onCreate(android.os.Bundle) (HelloJni.java:75)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.app.Activity.performCreate(android.os.Bundle, android.os.PersistableBundle) (Activity.java:7009)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:7000)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1214)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2731)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2856)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1589)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:106)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.os.Looper.loop() (Looper.java:164)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6494)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:438)
2019-04-27 21:04:25.650 21326-21326/com.example.hellojni I/zygote: at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:807)
2019-04-27 21:04:25.651 21326-21326/com.example.hellojni I/zygote: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
2019-04-27 21:04:25.651 21326-21326/com.example.hellojni I/zygote: at void android.support.v4.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
2019-04-27 21:04:25.651 21326-21326/com.example.hellojni I/zygote: at void android.support.v7.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
2019-04-27 21:04:25.651 21326-21326/com.example.hellojni I/zygote: at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2)
2019-04-27 21:04:25.651 21326-21326/com.example.hellojni I/zygote: at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:334)
2019-04-27 21:04:25.651 21326-21326/com.example.hellojni I/zygote: at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:647)
2019-04-27 21:04:25.651 21326-21326/com.example.hellojni I/zygote: at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:790)
2019-04-27 21:04:25.651 21326-21326/com.example.hellojni I/zygote: at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:730)
2019-04-27 21:04:25.651 21326-21326/com.example.hellojni I/zygote: at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:863)
2019-04-27 21:04:25.651 21326-21326/com.example.hellojni I/zygote: at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:824)