当前位置: 首页 > 知识库问答 >
问题:

我的应用程序在Nexus模拟器中运行良好,但在Pixel上却一直崩溃

羊毅庵
2023-03-14

我在Android中做了一个简单的计时器应用程序,就像只要过了10sec它就会给出一个消息“完成了!!!”。

文本视图中(计划添加嘟嘟声),同时在另一个文本视图中显示CountDownTimer。但我注意到我的应用程序在nexus模拟器上运行得很好,而在pixel模拟器上却崩溃了,

而且我的真手机(红米note 4)也一直崩溃,

这可能是什么原因?

我的代码:

package com.example.pickachu.timerapp;

import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.sql.Time;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final TextView time=(TextView)findViewById(R.id.Time);
        final TextView Status =(TextView)findViewById(R.id.Status);


       new CountDownTimer(10000, 1000) {

            @Override
            public void onTick(long l) {
                time.setText("SEC   :   "+String.valueOf(l/1000));
            }

            @Override
            public void onFinish() {
                Status.setText("DONE!!!");
                time.setText("SEC  :  0");
            }

        }.start();
    }
}

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/clock"
    android:backgroundTint="@android:color/background_light"
    tools:context="com.example.pickachu.timerapp.MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:contentDescription="@string/app_name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/clock" />

    <TextView
        android:id="@+id/Time"
        android:layout_width="188dp"
        android:layout_height="62dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/background_dark"
        android:paddingTop="12sp"
        android:text="@string/Time"
        android:textAlignment="center"
        android:textColor="#ffffff"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.369" />

    <TextView
        android:id="@+id/Status"
        android:layout_width="105dp"
        android:layout_height="48dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:paddingTop="11sp"
        android:text="@string/Status"
        android:textAlignment="center"
        android:textColor="#ffffff"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.567"
        tools:text="@string/Status" />

</android.support.constraint.ConstraintLayout>

Logcat错误消息:

02-05 10:49:51.001 4637-4637/com.example.pickachu.timerapp     E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.pickachu.timerapp, PID: 4637
  java.lang.RuntimeException: Canvas: trying to draw too large(128399328bytes) bitmap.
      at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)
      at android.graphics.Canvas.drawBitmap(Canvas.java:1415)
      at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:528)
      at android.view.View.getDrawableRenderNode(View.java:17428)
      at android.view.View.drawBackground(View.java:17364)
      at android.view.View.draw(View.java:17176)
      at android.view.View.updateDisplayListIfDirty(View.java:16167)
      at android.view.View.draw(View.java:16951)
      at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
      at android.view.View.updateDisplayListIfDirty(View.java:16162)
      at android.view.View.draw(View.java:16951)
      at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
      at android.view.View.updateDisplayListIfDirty(View.java:16162)
      at android.view.View.draw(View.java:16951)
      at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
      at android.view.View.updateDisplayListIfDirty(View.java:16162)
      at android.view.View.draw(View.java:16951)
      at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
      at android.view.View.updateDisplayListIfDirty(View.java:16162)
      at android.view.View.draw(View.java:16951)
      at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
      at android.view.View.draw(View.java:17188)
      at com.android.internal.policy.DecorView.draw(DecorView.java:753)
      at android.view.View.updateDisplayListIfDirty(View.java:16167)
      at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:648)
      at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:654)
      at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:762)
      at android.view.ViewRootImpl.draw(ViewRootImpl.java:2800)
      at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2608)
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2215)
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
      at android.view.Choreographer.doCallbacks(Choreographer.java:686)
      at android.view.Choreographer.doFrame(Choreographer.java:621)
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
      at android.os.Handler.handleCallback(Handler.java:751)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6119)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

图像

共有1个答案

夏振国
2023-03-14

试试这个...

因为真正的手机在运行时很难找到屏幕的可绘制图像。将您的图像放置到电话的分辨率(可绘制)文件夹。如果您通过所有设备运行应用程序,您应该将图像添加到所有分辨率(可绘制)文件夹。将可绘制的图像放置到drawable-xxhdpi文件夹中。

 类似资料: