我在我的活动中的一些设备上得到了ArithmeticException(除以0)。这是我得到的堆栈跟踪
java.lang.ArithmeticException: divide by zero
at com.afre.quirky.GameActivity.a(Unknown Source)
at com.afre.quirky.GameActivity.a(Unknown Source)
at com.afre.f.a.<init>(Unknown Source)
at com.afre.quirky.d.c(Unknown Source)
at com.afre.quirky.d.<init>(Unknown Source)
at com.afre.quirky.a.onClick(Unknown Source)
at android.view.View.performClick(View.java:4107)
at android.view.View$PerformClick.run(View.java:17166)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5559)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
at dalvik.system.NativeStart.main(Native Method)
我的包名是 com.afre.quirky ,我收到错误的活动是 GameActivity
奇怪的是,我在极少数设备上遇到此错误(HTC Desire 700就是其中之一)而且,堆栈跟踪并未显示GameActivity中的确切行,其中I& #39;我得到了错误。除此之外,我在该活动中没有任何除法任务,其中除数可以为零。
这是我的GameActivity代码
public class GameActivity extends AppCompatActivity {
private GameView gameView;
public static int SCREEN_HEIGHT;
public static int SCREEN_WIDTH;
ImageView image;
ImageView demo;
ImageView loading;
TextView loadText;
RelativeLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mLayout = (RelativeLayout) findViewById(R.id.gameLayout);
image = (ImageView) findViewById(R.id.color);
demo = (ImageView) findViewById(R.id.demo);
loading = (ImageView) findViewById(R.id.loading);
loadText = (TextView) findViewById(R.id.loadText);
setWidthAndHeight();
}
void setWidthAndHeight() {
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics size = new DisplayMetrics();
int navBarHeight = 0;
Resources resources = getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
navBarHeight = resources.getDimensionPixelSize(resourceId);
}
boolean hasPhysicalHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
if(Build.VERSION.SDK_INT >= 17) {
display.getRealMetrics(size);
SCREEN_WIDTH = size.widthPixels;
SCREEN_HEIGHT = size.heightPixels;
}
else if(hasPhysicalHomeKey) {
SCREEN_WIDTH = new DisplayMetrics().widthPixels;
SCREEN_HEIGHT = new DisplayMetrics().heightPixels;
}
else {
SCREEN_WIDTH = new DisplayMetrics().widthPixels;
SCREEN_HEIGHT = new DisplayMetrics().heightPixels + navBarHeight;
}
}
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="FullscreenTheme" parent="AppTheme">
<item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
</style>
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/black_overlay</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>