Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android基本编码尝试捕获显示Toast不工作?_Android_Image_Debugging_Error Handling_Try Catch - Fatal编程技术网

Android基本编码尝试捕获显示Toast不工作?

Android基本编码尝试捕获显示Toast不工作?,android,image,debugging,error-handling,try-catch,Android,Image,Debugging,Error Handling,Try Catch,我正在构建一个真正简单的Android应用程序,但很多地方都出了问题 我甚至连一次像样的尝试都做不到。由于我得到的机器动力严重不足,我直接在我的安卓设备上进行测试,但这并没有真正起作用 即使使用一般的try-catch,在执行基本操作后,应用程序似乎也会崩溃。我做错了什么 为什么试捕器没有捕捉到任何东西? 这是我的Main.xml(一个textview和另一个控件被剪掉) 解决方案 大小超过3Mb的文件。ImageButton不会显示“大”的图片,只是什么都不做。 应用程序可能会抛出一些随机内存

我正在构建一个真正简单的Android应用程序,但很多地方都出了问题

我甚至连一次像样的尝试都做不到。由于我得到的机器动力严重不足,我直接在我的安卓设备上进行测试,但这并没有真正起作用

即使使用一般的try-catch,在执行基本操作后,应用程序似乎也会崩溃。我做错了什么

为什么试捕器没有捕捉到任何东西?

这是我的Main.xml(一个textview和另一个控件被剪掉)

解决方案

大小超过3Mb的文件。ImageButton不会显示“大”的图片,只是什么都不做。 应用程序可能会抛出一些随机内存不足异常,加载图像会导致整个应用程序崩溃

读取logcat足以调试此“问题”

一些确实有效的代码(请借用其他SO问题)


要么您没有具有该id的
ImageButton
,要么您试图加载的图像太大,无法容纳缩减的应用程序内存。

我已经测试了您的代码片段

首先,ImageButton不会显示android:text部分,因为该属性属于Button

仅使用res/layout文件夹main.xml中的LinearLayout和ImageButton,这段代码就可以正常工作:

public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void onButtonClicked(View v) {
    try {
        String strFilePath = "/mnt/sdcard/somefile.png";
        if (strFilePath.length() > 0) {

            File file = new File(strFilePath);

            if (file.exists()) {
                //ShowToast(strFilePath + "Bestaat");

                ImageButton image = (ImageButton) findViewById(R.id.ib);
                Bitmap bMap = BitmapFactory.decodeFile(strFilePath);
                image.setImageBitmap(bMap);

                //ShowToast( "Done");
            } else {
                //ShowToast(strFilePath + "Bestaat NIET");
            }
        } else {
            //ShowToast(strFilePath + "strFilePath.length() =0");
        }

    } catch (Exception e) {
        //HandleError(e);
        e.printStackTrace();
    }
}
}

因此,要么文件像K-Ballo建议的那样大,要么ShowToast或HandleError方法有问题。

“应用程序似乎崩溃了”-如何?不再回应了?显示“强制关闭”对话框?还有其他错误消息吗?去掉Try-Catch块,这样你就能在日志中看到真正的错误了!要么您没有带有该id的
ImageButton
,要么您试图加载的图像太大,无法容纳缩减的应用程序内存。在这种情况下,您应该在logcat中有一个stacktrace-请发布。@CodingBarfield:这取决于您,你告诉编译器你会处理OutOfMemory或类似的异常,但你没有。根据logcat,图像被暗示为大到可以在ImageButton上显示,默认行为是执行而不显示。再加载几次后,应用程序崩溃,出现OutOfMemoryException,我无法触发任何备份方案,因为所有操作都需要一些内存。我只是在这里猜测,但是我希望
decodeFile
如果加载失败,至少会返回
null
。你能用一个3Mb左右的jpg文件来测试它吗?它在您的设备上做什么?它崩溃的原因:java.lang.OutOfMemoryError:android.graphics.BitmapFactory.NativeDecodeSeam(本机方法)上的位图大小超过android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)上的VM预算(BitmapFactory.java:284)位于android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:309)位于com.example.Main.onButtonClicked(Main.java:35)
public void onButtonClicked(View v) {
    try {
        String strFilePath = "/mnt/sdcard/somefile.jpg";
        if (strFilePath.length() > 0) {

            File file = new File(strFilePath);

            if (file.exists()) {
                ShowToast(strFilePath + "Bestaat");

                ImageButton image = (ImageButton) findViewById(R.id.ib);
                Bitmap bMap = BitmapFactory.decodeFile(strFilePath);
                image.setImageBitmap(bMap);

                ShowToast( "Done");
            } else {
                ShowToast(strFilePath + "Bestaat NIET");
            }
        } else {
            ShowToast(strFilePath + "strFilePath.length() =0");
        }

    } catch (Exception e) {
        HandleError(e);
    }
}
    ImageButton image = (ImageButton) findViewById(R.id.ib);

    Bitmap bMap = BitmapFactory.decodeFile(strFilePath);
    Bitmap bSized = getResizedBitmap(bMap,150,150);
    image.setImageBitmap(bSized);
    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)                     
    {
      int width = bm.getWidth();
      int height = bm.getHeight();
      float scaleWidth = ((float) newWidth) / width;
      float scaleHeight = ((float) newHeight) / height;

      // create a matrix for the manipulation
      Matrix matrix = new Matrix();

      // resize the bit map
      matrix.postScale(scaleWidth, scaleHeight);

      // recreate the new Bitmap
      Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
      return resizedBitmap;

}
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void onButtonClicked(View v) {
    try {
        String strFilePath = "/mnt/sdcard/somefile.png";
        if (strFilePath.length() > 0) {

            File file = new File(strFilePath);

            if (file.exists()) {
                //ShowToast(strFilePath + "Bestaat");

                ImageButton image = (ImageButton) findViewById(R.id.ib);
                Bitmap bMap = BitmapFactory.decodeFile(strFilePath);
                image.setImageBitmap(bMap);

                //ShowToast( "Done");
            } else {
                //ShowToast(strFilePath + "Bestaat NIET");
            }
        } else {
            //ShowToast(strFilePath + "strFilePath.length() =0");
        }

    } catch (Exception e) {
        //HandleError(e);
        e.printStackTrace();
    }
}
}