尝试使用if语句设置图像资源时强制关闭

时间:2016-09-27 06:20:28

标签: android android-studio

您好我的教授无法解决这个问题所以我想我会在这里发帖。我正在尝试创建一个显示温度的应用程序,当搜索栏达到某个临时值时,我希望在imageView中显示不同的图像。当我测试应用程序并且搜索栏超过40时,应用程序取消预审。谁能告诉我为什么会这样?

SeekBar seekBar;
TextView f, c;
ImageView i;
int progress_value;


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

    seekBar = (SeekBar)findViewById(R.id.seekBar);
    seekBar.setMax(200);
    f = (TextView)findViewById(R.id.f);
    c = (TextView)findViewById(R.id.c);
    i = (ImageView)findViewById(R.id.imageView);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progress_value = progress;

            f.setText(" " + progress_value);
            c.setText(" " + .55 * (progress_value - 32));

            if (progress_value <= 40){
                i.setImageResource(R.drawable.cold);
            }
            else if (progress_value > 40 && progress_value < 90){
                i.setImageResource(R.drawable.justright);
            }
            else{
                i.setImageResource(R.drawable.hot);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

}

}

09/26 23:21:56:启动应用 $ adb push C:\ Users \ aniel \ AndroidStudioProjects \ HomeWork#2 \ app \ build \ outputs \ apk \ app-debug.apk /data/local/tmp/com.example.aniel.homework2 $ adb shell pm install -r&#34; /data/local/tmp/com.example.aniel.homework2" 成功

$ adb shell am start -n&#34; com.example.aniel.homework2 / com.example.aniel.homework2.Tempature&#34; -a android.intent.action.MAIN -c android.intent.category.LAUNCHER 客户尚未准备好......等待流程上线 等待进程上线 等待进程上线 等待进程上线 等待进程上线 连接到设备模拟器-5554上的过程2373 W / gralloc_ranchu:Gralloc管道失败

              [ 09-26 23:22:05.204  2373: 2373 D/         ]
              HostConnection::get() New Host Connection established 0x982851c0, tid 2373

I / OpenGLRenderer:初始化的EGL,版本1.4 D / OpenGLRenderer:交换行为1 D / AndroidRuntime:关闭VM

              --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.aniel.homework2, PID: 2373
                  java.lang.RuntimeException: Canvas: trying to draw too large(241864000bytes) 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:545)
                      at android.widget.ImageView.onDraw(ImageView.java:1286)
                      at android.view.View.draw(View.java:17068)
                      at android.view.View.updateDisplayListIfDirty(View.java:16050)
                      at android.view.View.draw(View.java:16834)
                      at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                      at android.view.View.updateDisplayListIfDirty(View.java:16045)
                      at android.view.View.draw(View.java:16834)
                      at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                      at android.view.View.updateDisplayListIfDirty(View.java:16045)
                      at android.view.View.draw(View.java:16834)
                      at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                      at android.view.View.updateDisplayListIfDirty(View.java:16045)
                      at android.view.View.draw(View.java:16834)
                      at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                      at android.view.View.updateDisplayListIfDirty(View.java:16045)
                      at android.view.View.draw(View.java:16834)
                      at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                      at android.view.View.updateDisplayListIfDirty(View.java:16045)
                      at android.view.View.draw(View.java:16834)
                      at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                      at android.view.View.draw(View.java:17071)
                      at com.android.internal.policy.DecorView.draw(DecorView.java:751)
                      at android.view.View.updateDisplayListIfDirty(View.java:16050)
                      at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:656)
                      at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:662)
                      at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:770)
                      at android.view.ViewRootImpl.draw(ViewRootImpl.java:2796)
                      at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2604)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2211)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                      at android.view.Choreographer.doFrame(Choreographer.java:619)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                      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:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.

2 个答案:

答案 0 :(得分:0)

可绘制的图像无法在运行时更改。因此,您的应用程序崩溃了。 我想提出一个修复方案。尝试将可绘制图像转换为位图,然后根据所需条件更改位图。

答案 1 :(得分:0)

使用Picasso,这样的东西对我有用

ImageView img1 = (ImageView)findViewById(R.id.chamb_img);
Picasso.with(this).load(R.drawable.h).resize(75,75).into(img1);