0

私は本当にシンプルなAndroidアプリを構築していますが、多くの問題が発生しています。

私はまともなTryCatchEverythingを実行することさえできません。そして、私が手に入れたマシンは非常にパワーが不足しているので、Androidデバイスで直接テストしていますが、実際には機能していません。

一般的なtrycatchを使用しても、基本的なアクションを実行した後、アプリケーションがクラッシュするようです。私はここで何を間違っているのですか。

トライキャッチが何もキャッチしないのはなぜですか?

これは私のMain.xmlです(テキストビューと別のコントロールが切り取られています)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

 <ImageButton
        android:id="@+id/ib"
        android:layout_width="300dip"
        android:layout_height="50dip"
        android:text="Hello World, MyActivity"
        android:scaleType="fitXY"
        android:onClick="onButtonClicked"
        />

これは私の単純なコードです(そして画像は表示されていません)

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);
    }
}

解決

サイズが3Mbを超えるファイル。ImageButtonは、「大きい」画像を表示せず、単に何もしません。アプリケーションはおそらく、アプリ全体を停止する画像をロードする際に、ランダムなメモリ不足の例外をスローしました。

logcatを読み取るだけで、この「問題」をデバッグできました。

動作するコード(他のSOの質問から親切に借りたもの)

    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;

}
4

2 に答える 2

1

コードスニペットをテストしました。

まず、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メソッドに問題があります。

于 2012-05-20T20:17:57.733 に答える
1

そのIDを持つがないかImageButton、ロードしようとしているイメージが大きすぎて、削減されたアプリケーションメモリに収まりません。

于 2012-05-20T20:14:11.427 に答える