2009-12-21 8 views
0

나는 Java뿐 아니라 Android에서도 매우 새로운 기능을 제공합니다. 사이트에서 이미지 URL을 가져 와서 장치에 다운로드하고 나중에 사용자가 배경 화면으로 설정할 수있게하려는 응용 프로그램을 개발했습니다. 그러나 버튼에 onclick 이벤트를 할당 할 때 문제가 발생했습니다.

빨간색 줄을 주석 처리 해제하면 응용 프로그램이 예기치 않게 중지되었다는 상자가 팝업됩니다. 누군가 이걸로 나를 도울 수 있습니까?버튼에 onclick을 할당 할 때 강제 종료

private ImageView imView = null; 

public void onCreate(Bundle icicle) { 
super.onCreate(icicle); 
setContentView(R.layout.main); 

try { 
/* Create a URL we want to load some xml-data from. */ 
URL url = new URL(xmlURL); 
/* Get a SAXParser from the SAXPArserFactory. */ 
SAXParserFactory spf = SAXParserFactory.newInstance(); 
SAXParser sp = spf.newSAXParser(); 

/* Get the XMLReader of the SAXParser we created. */ 
XMLReader xr = sp.getXMLReader(); 
/* Create a new ContentHandler and apply it to the XML-Reader */ 
ExampleHandler myExampleHandler = new ExampleHandler(); 
xr.setContentHandler(myExampleHandler); 

/* Parse the xml-data from our URL. */ 
xr.parse(new InputSource(url.openStream())); 
/* Parsing has finished. */ 

/* Our ExampleHandler now provides the parsed data to us. */ 
ParsedExampleDataSet parsedExampleDataSet = myExampleHandler 
.getParsedData(); 

/* Set the result to be displayed in our GUI. */ 
if (myExampleHandler.filenames != null) { 
a = a + "\n" + myExampleHandler.filenames + ", by " 
+ myExampleHandler.authors + "\nhits: " 
+ myExampleHandler.hits + " downloads"; 
this.ed = myExampleHandler.thumbs; 
this.imageURL = myExampleHandler.mediafiles; 
} 
} catch (Exception e) { 
a = e.getMessage(); 
} 

// get thumbnail 
Context context = this.getBaseContext(); 
if (ed.length() != 0) { 
Drawable image = ImageOperations(context, this.ed, "image.jpg"); 
ImageView imgView = new ImageView(context); 
imgView = (ImageView) findViewById(R.id.image1); 
imgView.setImageDrawable(image); 
} 

TextView tv = (TextView) findViewById(R.id.txt_name); 
tv.setText(a); 

Button bt3 = (Button) findViewById(R.id.get_imagebt); 
//bt3.setOnClickListener(getImageBtnOnClick); 
} 

OnClickListener getImageBtnOnClick = new OnClickListener() { 
public void onClick(View view) { 
downloadFile(imageURL); 
} 
}; 

void downloadFile(String fileUrl) { 
     URL myFileUrl = null; 
     try { 
      myFileUrl = new URL(fileUrl); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      HttpURLConnection conn = (HttpURLConnection) myFileUrl 
        .openConnection(); 
      conn.setDoInput(true); 
      conn.connect(); 
      int length = conn.getContentLength(); 

      InputStream is = conn.getInputStream(); 

      bmImg = BitmapFactory.decodeStream(is); 
      // this.imView.setImageBitmap(bmImg); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

private Drawable ImageOperations(Context ctx, String url, 
      String saveFilename) { 
     try { 
      InputStream is = (InputStream) this.fetch(url); 
      Drawable d = Drawable.createFromStream(is, "src"); 
      return d; 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      return null; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 
public Object fetch(String address) throws MalformedURLException, 
     IOException { 
    URL url = new URL(address); 
    Object content = url.getContent(); 
    return content; 
} 

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" android:id="@+id/viewgroup"> 
    <ImageView android:id="@+id/image1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" /> 
    <ImageView android:id="@+id/image2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" /> 
    <TextView android:id="@+id/txt_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" /> 
    <Button id="@+id/get_imagebt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="xxx Get an image" 
     android:layout_gravity="center" /> 
    <ImageView id="@+id/imview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" /> 
</LinearLayout> 
+0

안녕하세요, logcat을 사용하여 자세한 정보를 찾을 수 있습니다. 명령 프롬프트 (시작 ... 실행 ... 'cmd')를 열고 안드로이드 SDK 디렉토리로 이동하십시오 (환경 PATH에 있지 않은 경우). 도구 폴더로 이동하여 "adb.exe logcat"을 입력하십시오. 그런 다음 응용 프로그램을 시작하고 프롬프트에서 출력을 모니터하십시오. 그것은 예외를 던질 것이고 그 세부 사항과 더불어, 문제를 좁히는 것이 더 쉽습니다. – keyboardP

+0

E/AndroidRuntime (711) : catch되지 않은 핸들러 : 인해 Catch되지 않는 전자 에 종료 주 스레드 xception E/AndroidRuntime (711) : java.lang.RuntimeException가 : 활동 공동 mponentInfo {resonet.example.getImage/resonet를 시작할 수 없습니다. example.getImage.getImage} : java.la ng.NullPointerException – Lynnooi

+0

E/AndroidRuntime (711) : android.app.ActivityThread.performLaunchActiv 성만 (ActivityThread.java:2268) E/AndroidRuntime (711)에서 : 안드로이드에서. app.ActivityThread.handleLaunchActivi 타이 (ActivityThread.java:2284) E/AndroidRuntime (711)에서 android.app.ActivityThread.access $ 1800 (Activi tyThread.java:112) E/AndroidRuntime (711) : 안드로이드에서 .app.A ctivityThread $ H.handleMessage (AC tivityThread.java:1692) – Lynnooi

답변

0

, 오류 메시지에 대한 catlog보기를 확인하시기 바랍니다. nullPointerException이라면 id가 "get_imagebt"인 버튼이 컨텐츠 뷰로 설정 한 파일의 일부가 아닌 것입니까? 또한 main.xml 레이아웃 파일을 게시 할 수 있습니까?
감사합니다!

+0

나는 catlog를 확인했고 nullPointerException을 발견했다. 따라서 어떻게 해결할 수 있습니까? 도와 주셔서 정말로 고맙습니다. – Lynnooi

0

게시 한 코드에 downloadFile() 함수가 표시되지 않습니다. 그것도 붙여 넣으면 도움이 될 것입니다. 또한, 당신이 얻고있는 예외를 지정할 수 있습니까 (logcat 확인)?

내가 생각할 수있는 문제는 매니페스트 파일에서 인터넷 권한이 누락되었을 수 있습니다.

+0

나는 다운로드 파일 기능을 추가했다. 인터넷 사용 권한에 대해서는 이미 내 매니페스트 파일에 포함되어있었습니다. 원격 사이트에서 필요한 데이터를 얻을 수 있습니다. – Lynnooi

관련 문제