2012-05-20 2 views
0

나는 진짜 간단한 안드로이드 애플 리케이션을 만들고있어 많은 것이 잘못되어 가고있다.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; 

} 
+1

* "응용 프로그램이 충돌하는 것 같습니다."* - 어떻게? 더 이상 응답하지 않습니까? "강제 닫기"대화 상자를 표시합니까? 다른 오류 메시지가 있습니까? –

+1

LogCat에서 실제 오류가 표시 될 때까지 Try Catch 블록을 제거하십시오! – Blundell

+1

그 id를 가진'ImageButton'을 가지고 있지 않거나,로드하려는 이미지가 축소 된 응용 프로그램 메모리에 적합하도록 커집니다. –

답변

1

어느 쪽이든 당신 돈을 (친절하게 다른 SO 질문에서 차용)이 '문제'

작업을 수행 일부 코드를 디버깅에 충분했다 해당 ID가 ImageButton이거나로드하려는 이미지가 축소 된 응용 프로그램 메모리에 적합하도록 커야합니다.

+0

logcat에 따르면 이미지는 ImageButton에 표시 할 큰 것을 의미하며 기본 동작은 수행하고 표시하지 않는 것입니다. 몇 번로드 한 후 앱이 OutOfMemoryException으로 추락하여 모든 백업 비용이 발생하기 때문에 모든 백업 시나리오를 트리거 할 수있는 변경 사항이 없습니다. – CodingBarfield

+1

저는 여기서 추측하고 있습니다 만, 무언가를로드하지 못하면'decodeFile'가 적어도'null'을 반환 할 것으로 기대합니다. –

1

난 당신의 코드 조각을 테스트 한 .. 모든

첫째는하여 ImageButton이 안드로이드 표시되지 않습니다 : 그 속성은 버튼에 속하기 때문에 텍스트 부분을 .. 그냥있는 LinearLayout과하여 ImageButton에서와

입술/레이아웃 폴더에있는 main.xml에이 코드가 잘 작동 :

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-가면 무도회 '가 제안 또는 무언가가 당신의 ShowToast 또는는 handleError 방법에 문제가 있습니다로 큰이다.

+0

3Mb 크기의 jpg 파일을 사용하여 테스트 할 수 있습니까? 기기에서 무엇을합니까? – CodingBarfield

+1

그것은으로 추락 java.lang.OutOfMemoryError와 :에 의한 비트 맵의 ​​크기는 android.graphics.BitmapFactory.decodeStream (BitmapFactory.java:470) 에서 android.graphics.BitmapFactory.nativeDecodeStream (기본 방법) 에서 VM 예산 을 초과 android.graphics.BitmapFactory.decodeFile (BitmapFactory.java:284) android.graphics.BitmapFactory.decodeFile (BitmapFactory.java:309) at com.example.Main.onButtonClicked (Main.java:35) – EivinGS