나는 진짜 간단한 안드로이드 애플 리케이션을 만들고있어 많은 것이 잘못되어 가고있다.Android 기본 코딩 캐치 쇼 시도 토스트가 작동하지 않습니까?
필자도 모든 것을 실행 해보십시오. 그리고 내가 가지고있는 기계가 매우 힘이 부족하기 때문에 나는 안드로이드 장치를 직접 테스트하고 있지만 실제로 작동하지는 않습니다.
일반적인 try catch를 사용하여 기본적인 작업을 수행 한 후에 응용 프로그램이 충돌하는 것 같습니다. 내가 여기서 잘못하고있는 것은 무엇인가.
왜 try catch가 아무 것도 잡히지 않는 이유는 무엇입니까?
이 내 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은 '크고'단순히 아무것도하지 않는 그림을 표시하지 않습니다. 응용 프로그램은 전체 응용 프로그램을 다운시키는 이미지를로드 할 때 메모리 예외를 벗어났습니다. 로그 캣 읽기
는
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;
}
* "응용 프로그램이 충돌하는 것 같습니다."* - 어떻게? 더 이상 응답하지 않습니까? "강제 닫기"대화 상자를 표시합니까? 다른 오류 메시지가 있습니까? –
LogCat에서 실제 오류가 표시 될 때까지 Try Catch 블록을 제거하십시오! – Blundell
그 id를 가진'ImageButton'을 가지고 있지 않거나,로드하려는 이미지가 축소 된 응용 프로그램 메모리에 적합하도록 커집니다. –