如果以apk形式安装,则Android应用程序崩溃;如果通过Android Studio运行,则该应用程序可以工作 [英] Android app crashes if installed as apk, works if runned trough Android Studio

查看:124
本文介绍了如果以apk形式安装,则Android应用程序崩溃;如果通过Android Studio运行,则该应用程序可以工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android Studio中2.1.3开发的应用,那淡然的我在(不同的),仿真器和我自己的手机(小米红米手机注3,安卓5.0.2 LRX22G)测试了.当我将应用程序交给几个朋友测试时,他们都说了同样的话:应用程序正常启动,他们选择了他们感兴趣的活动,他们在文本字段中输入了数据,并在按下按钮的那一刻(这是假定的).使一些数学),以接收数学答案,该应用停止,并返回到主"的活性.

I made an app in Android Studio 2.1.3, that of couse I have tested both in (different) emulators and my own phone (Xiaomi Redmi Note 3, Android 5.0.2 LRX22G). When I gave the apps to a few friends to test they all said the same thing : the app starts normally, they select the activity that they are interested in, they input the data in the textfields and the moment they press the button (that supposes to make some math) to receive the math answers, the app stops and returns to the "main" activity.

compileSdkVersion 24 
buildToolsVersion '23.0.3' 
dexOptions { javaMaxHeapSize "4g" } 
defaultConfig { applicationId "com.example.android.rechner" minSdkVersion 17 
targetSdkVersion 24 
versionCode 4 
versionName 'v.1.6.0 - Entwickler Stufe V.16.9.03' 
multiDexEnabled true

我买了一个平板电脑,希望能够将相同的崩溃连接到android studio并进行调试.我通过BT将apk(调试apk,未发布)发送到平板电脑(Teclast Tpad X80 Pro(E3E7),Android 5.1),安装了该应用程序,并按预期工作! 现在我生气了! :) 我去找另一个朋友做和平板电脑一样的事情,但是把文件发送给了三星Galaxy Grand Prime(看起来没有什么android版本),当我尝试做数学运算时崩溃了!他给了我一个三星Galaxy S3 Mini(GT-I8200N,Android 4.2.2),我通过BT发送APK,发生了同样的事情!

I bought a tablet hoping to get the same crash connected to android studio and get the chance to debug. I sent the apk (debug apk, not release) over BT to the tablet (Teclast Tpad X80 Pro (E3E7), Android 5.1 ), installed the app, and works as it supposed to ! Now I'm getting pissed off ! :) I went to another friend did the same thing as with the tablet, but sent the file to a Samsung Galaxy Grand Prime (didn't look what android version does it have), an crashes when I try to do the math ! He gives me an Samsung Galaxy S3 Mini (GT-I8200N, Android 4.2.2), I send the APK over BT, SAME THING HAPPENS !

好吧,我开始高兴了,因为我随身带着手机,启用了开发人员选项,通过USB进行调试,将手机连接到我的计算机,使用Android Studio加载应用程序,然后猜猜是什么?!它可以正常工作,没有任何错误,折痕或其他任何东西! 我将手机重置为出厂设置,希望强制执行此错误...我通过BT发送了文件,并且...工作正常!尽管重置后手机没有连接到我的计算机(和Android Studio),但没有任何错误或崩溃.我看不到问题,并且无法在调试日志中寻找答案,因为连接到PC不会失败:(

Well, I'm starting to be happy because I took the phone with me, I enable developer options, debugging over USB, connect the phone to my computer, load the app with Android Studio, and guess what ?! IT WORKS without any errors, or crases or anything ! I reset the phone to factory settings, hoping to force the error ... I sent the file over BT, and ... IT WORKS ! No errors or crashes whatsoever, though after reset the phone did not connect to my computer (and Android Studio). I fail to see the problem and can't look for answers in a debug log since connected to PC doesn't fail :(

我需要提及的是,在我测试了该应用的其他手机上,我启用了开发人员选项,但是唯一的区别是,这些选项都没有连接到我的计算机和Android Studio上!

I need to mention that I enabled developer options on the other phones I have tested the app, but the only difference is that none of them were connected to my computer and Android Studio !

你们中的任何人以前遇到过此问题吗?我已经在Google朋友中搜索了几天类似的内容,但没有找到任何内容.

Have any of you encountered this problem before ? I have searched for days on our google friend for something similar but didn't found anything.

感谢您的耐心等待和解答!

Thank you for your patience and answers !

最好的问候,罗伯特P上.

Best regards, Robert P.

L.E.我已经刷新了手机,擦除了缓存和数据,重新安装了apk,最终崩溃了.我成功读取了adb logcat:

L.E. I have reflashed the phone, erased cache and data, reinstalled the apk, and finnaly crashes. I succeded to read adb logcat :

W/dalvikvm( 4700): threadid=1: thread exiting with uncaught exception (group=0x40d7d960)
E/AndroidRuntime( 4700): FATAL EXCEPTION: main
E/AndroidRuntime( 4700): java.lang.StringIndexOutOfBoundsException: length=4; regionStart=0; regionLength=-1
E/AndroidRuntime( 4700):        at java.lang.String.startEndAndLength(String.java:583)
E/AndroidRuntime( 4700):        at java.lang.String.substring(String.java:1464)
E/AndroidRuntime( 4700):        at com.example.android.rechner.MainActivityRustZ.displayTimeReal(MainActivityRustZ.java:109)
E/AndroidRuntime( 4700):        at com.example.android.rechner.MainActivityRustZ.access$200(MainActivityRustZ.java:12)
E/AndroidRuntime( 4700):        at com.example.android.rechner.MainActivityRustZ$1.onClick(MainActivityRustZ.java:29)
E/AndroidRuntime( 4700):        at android.view.View.performClick(View.java:4452)
E/AndroidRuntime( 4700):        at android.widget.Button.performClick(Button.java:148)
E/AndroidRuntime( 4700):        at android.view.View$PerformClick.run(View.java:18428)
E/AndroidRuntime( 4700):        at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime( 4700):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 4700):        at android.os.Looper.loop(Looper.java:176)
E/AndroidRuntime( 4700):        at android.app.ActivityThread.main(ActivityThread.java:5365)
E/AndroidRuntime( 4700):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 4700):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 4700):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
E/AndroidRuntime( 4700):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
E/AndroidRuntime( 4700):        at dalvik.system.NativeStart.main(Native Method)

现在,第109行: 字符串unitatiTimpReal = formattedTimpR.substring(0,formattedTimpR.indexOf(.")); //单位

now, line 109 : String unitatiTimpReal = formattedTimpR.substring(0, formattedTimpR.indexOf(".")); //unit

是的一部分:

private void displayTimeReal(double timpreal) {
    Double vZeitD = Double.parseDouble(((EditText) findViewById(R.id.vZeit)).getText().toString());
    Double aufStZahl = Double.parseDouble(((EditText) findViewById(R.id.aufGesSt)).getText().toString());
    timpreal = aufStZahl / ((procente / vZeitD) * mitarbeiter);
    DecimalFormat formattr = new DecimalFormat();
    formattr.setMaximumFractionDigits(2);
    formattr.setMinimumFractionDigits(2);
    String formattedTimpR = formattr.format(timpreal);
    String zecimaleTimpReal = formattedTimpR.substring(formattedTimpR.indexOf(".") + 1); //decimals
    String unitatiTimpReal = formattedTimpR.substring(0, formattedTimpR.indexOf(".")); //unit
    double minutereal = Double.parseDouble(zecimaleTimpReal);
    minutereal = minutereal * 0.6;
    DecimalFormat format = new DecimalFormat();
    format.setMaximumFractionDigits(2);
    String formattedMinuteReal = format.format(minutereal);
    TextView timeTextView = (TextView) findViewById(R.id.rezultat_ore_real);
    timeTextView.setText(getString(R.string.slaufz) + unitatiTimpReal + getString(R.string.shours) + formattedMinuteReal + getString(R.string.sminutes));
}

现在,同一行,相同的操作,相同的apk ...但是,如果我仅通过Android Studio运行一次该应用程序,直到完全重写固件并删除缓存,该应用程序将运行没有崩溃!

Now, the same line is there, the same operation is there, the same apk ... But if I will run the app ONLY once trough Android Studio, until a complete firmware rewrite and cache delete, the app will be running without the crash !

根据建议进行更新后: 这行:

after updating as suggested : this line :

String unitatiTimpReal = formattedTimpR.substring(0, formattedTimpR.indexOf(".")); //unit

进入:

String unitatiTimpReal;
int index = formattedTimpR.indexOf(".");
if(index != -1) {
unitatiTimpReal = formattedTimpR.substring(0, index);
} else {
// formattedTimpR does not contain ".", handle this somehow
unitatiTimpReal = "";
}

我明白了:

W/dalvikvm( 3518): threadid=1: thread exiting with uncaught exception (group=0x40cf4960)
E/AndroidRuntime( 3518): FATAL EXCEPTION: main
E/AndroidRuntime( 3518): java.lang.NumberFormatException: Invalid double: "3,32"
E/AndroidRuntime( 3518):        at java.lang.StringToReal.invalidReal(StringToReal.java:63)
E/AndroidRuntime( 3518):        at java.lang.StringToReal.parseDouble(StringToReal.java:269)
E/AndroidRuntime( 3518):        at java.lang.Double.parseDouble(Double.java:295)
E/AndroidRuntime( 3518):        at com.example.android.rechner.MainActivityRustZ.displayTimeReal(MainActivityRustZ.java:118)
E/AndroidRuntime( 3518):        at com.example.android.rechner.MainActivityRustZ.access$200(MainActivityRustZ.java:12)
E/AndroidRuntime( 3518):        at com.example.android.rechner.MainActivityRustZ$1.onClick(MainActivityRustZ.java:29)
E/AndroidRuntime( 3518):        at android.view.View.performClick(View.java:4452)
E/AndroidRuntime( 3518):        at android.widget.Button.performClick(Button.java:148)
E/AndroidRuntime( 3518):        at android.view.View$PerformClick.run(View.java:18428)
E/AndroidRuntime( 3518):        at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime( 3518):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 3518):        at android.os.Looper.loop(Looper.java:176)
E/AndroidRuntime( 3518):        at android.app.ActivityThread.main(ActivityThread.java:5365)
E/AndroidRuntime( 3518):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3518):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 3518):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
E/AndroidRuntime( 3518):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
E/AndroidRuntime( 3518):        at dalvik.system.NativeStart.main(Native Method)

第118行是:

        double minutereal = Double.parseDouble(zecimaleTimpReal);

@ user13 我想什么来实现: 在我为用户执行的其他一些计算中,基于输入的数据,我实时转换了工业时间(以分钟,小时为单位的百分比时间(更确切地说是正常时间为60分钟的100分钟工业时间)

@user13 What I want to achieve : Among a few other calculations that I do for the user, based on the data it inputs, I transform industrial time in real time (procentual time in minutes, hours (more exactly 100 minutes industrial time in 60 minutes normal time)

private void displayTimeReal(double timpreal) {

将var vZeitD初始化为双精度,要求该用户输入数学运算

initialising var vZeitD as double, that user is required to input for the math operation

Double vZeitD = Double.parseDouble(((EditText) findViewById(R.id.vZeit)).getText().toString());

将var aufStZahl初始化为双精度,要求该用户输入数学运算-在这里我可以修改整数,因为只允许输入整数

initialising var aufStZahl as double, that user is required to input for the math operation - here I can modify in integer because only integers are allowed to be input

Double aufStZahl = Double.parseDouble(((EditText) findViewById(R.id.aufGesSt)).getText().toString());

数学完成,以显示所需要的时间(但在工业时间,即100分钟)

The math done to show the time required (but is in industrial time, that is 100 minutes)

timpreal = aufStZahl / ((procente / vZeitD) * mitarbeiter);

我分裂应答(如实施例12.75,其中12是小时和75分钟),并予设定的最小显示小数最大值(2):

I split up the answer (as example 12.75, where 12 is hours and 75 are the minutes) and I set the maximum an minimum displayed decimals (2) :

DecimalFormat formattr = new DecimalFormat();
formattr.setMaximumFractionDigits(2);
formattr.setMinimumFractionDigits(2);
String formattedTimpR = formattr.format(timpreal);

取小数到从工业

taking the decimals to be converted from industrial

    String zecimaleTimpReal = formattedTimpR.substring(formattedTimpR.indexOf(".") + 1); //decimals

在完成数学运算后,从结果数字中获取单位(单位仍保持原样显示

taking the units from the resulted number after the math done (units remain to be displayes as they are

String unitatiTimpReal = formattedTimpR.substring(0, formattedTimpR.indexOf(".")); //unit

由于我必须再次做数学运算,所以我不能使用字符串,而是将小数转换为双精度,然后转换为实时(60分钟)

since I have to do math again, I can't use strings and I convert the decimals in double and I do the conversion to real time (60 minutes)

double minutereal = Double.parseDouble(zecimaleTimpReal);
minutereal = minutereal * 0.6;

我再次设置分钟的最大小数位数(这样就不会显示类似39.326562365的响应)(例如:39.33分钟而不是39.326562365)

I set again the maximum number of decimals (so that I don't show a response like 39.326562365) for the minutes (ex : 39.33 minutes instead of 39.326562365)

DecimalFormat format = new DecimalFormat();
format.setMaximumFractionDigits(2);
String formattedMinuteReal = format.format(minutereal);

显示该结果给用户:

displaying the result to the user :

TextView timeTextView = (TextView) findViewById(R.id.rezultat_ore_real);
timeTextView.setText(getString(R.string.slaufz) + unitatiTimpReal + getString(R.string.shours) + formattedMinuteReal + getString(R.string.sminutes));
}

推荐答案

问题似乎是由于数字根据设备的

The problem seems to be caused by the numbers being formatted differently based on the devices's Locale.

当您使用 DecimalFormat 对象时,小数点分隔符会有所不同取决于设备的语言设置.例如在英语中,它是一个点,例如3.14159,但在德语中是逗号,例如3,14159.

When you use a DecimalFormat object, the decimal separator is different depending on the device's language settings. For example in English it's a dot e.g. 3.14159 but in German it's a comma e.g. 3,14159.

这会导致问题,因为在调用indexOf()时,您的代码总是在寻找一个点.

This causes a problem because your code is always looking for a dot when you call indexOf().

您可以通过几种方法解决此问题.一种方法是在创建DecimalFormat对象时指定语言环境,例如:

You can fix this several ways. One way is to specify a locale when creating the DecimalFormat object, e.g:

NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
DecimalFormat formattr = (DecimalFormat)nf;

否则,您可以设置小数点分隔符:

Or else you can just set the decimal separator:

DecimalFormat formattr = new DecimalFormat();
formattr.setDecimalSeparator('.');

但是,也许您希望它为德国用户显示不同的内容.在这种情况下,您可以更改检查点的代码,以检查是否有正确的字符:

However perhaps you want it to display differently for German users. In that case you can change your code that checks for the dot to check for whatever the correct character is:

String zecimaleTimpReal = formattedTimpR.substring(formattedTimpR.indexOf("" + formattr.getDecimalFormatSymbols().getDecimalSeparator()) + 1);

添加到空字符串只是一个特技从炭转换为字符串.

Adding to the empty string is just a trick to convert from a char to a String.

请注意,还有一个分组符号",它也可以是逗号或点. 123,456.78.您可能要禁用分组,例如:

Be aware that there is also a "grouping symbol" which can be a comma or dot as well e.g. 123,456.78. You might want to disable grouping, like this:

formattr.setGroupingUsed(false);

这篇关于如果以apk形式安装,则Android应用程序崩溃;如果通过Android Studio运行,则该应用程序可以工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆