2011-10-23 89 views
1

我是剛剛接觸Android世界的人員,剛剛開始了一些示例以獲得理解。在這個例子中,我試圖從url中顯示圖像,但是當我啓動模擬器並單擊應用程序圖標時,它強制關閉。Android應用程序部隊關閉

the java file is:- 



    import java.io.InputStream; 
    import java.net.URL; 
    import android.app.Activity; 
    import android.graphics.drawable.Drawable; 
    import android.os.Bundle; 
    import android.widget.ImageView; 

    public class Lab1Activity extends Activity{ 
     /** Called when the activity is first created. */ 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ImageView imgView =(ImageView)findViewById(R.id.ImageView01); 
     Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png"); 
     imgView.setImageDrawable(drawable); 

     } 

     private Drawable LoadImageFromWebOperations(String url) 
     { 
     try 
     { 
     InputStream is = (InputStream) new URL(url).getContent(); 
     Drawable d = Drawable.createFromStream(is, "src name"); 

     return d; 
     }catch (Exception e) { 
     System.out.println("Exc="+e); 
     return null; 
     } 
     } 
     } 

Android清單: -

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="cmpe235.lab1" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="8" /> 


    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".Lab1Activity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 


</manifest> 

的main.xml: -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 
<ImageView android:id="@+id/ImageView01" 
android:layout_height="wrap_content" android:layout_width="wrap_content"/> 
</LinearLayout> 

在控制檯DDMS我得到這個錯誤: -

[2011-10-23 12:13:51 - ddms]null 
java.lang.NullPointerException 
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213) 
    at com.android.ddmlib.Client.sendAndConsume(Client.java:574) 
    at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142) 
    at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65) 
    at com.android.ddmlib.Client.getJdwpPacket(Client.java:671) 
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317) 
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263) 

那麼如何處理這個空指針訪問?謝謝

+0

stacktrace與你的代碼沒有任何關係,它甚至不包含你的類'Lab1Activity'。 logcat中是否存在實際涉及'Lab1Activity'的錯誤? –

回答

1

應用程序啓動時我就不會下載圖像,proberly導致異常:

Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png"); 
    imgView.setImageDrawable(drawable); 

保存圖像到你的3個RE /繪製的文件夾並加載它這樣,如果你想把它當背景:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout android:id="@+id/LinearLayout01"  
android:layout_width="fill_parent"  
android:layout_height="fill_parent" 
android:background="@drawable/youresavedfile" 
</LinearLayout> 
0

我可以看到NullPointerException的兩個可能位置都在您將drawable設置爲圖像的行中。它們可能是因爲你的圖像視圖爲空,或者因爲drawable爲空。我建議你在操作之前檢查圖像視圖是否爲空,並檢查是否在你的下載方法中引發了異常。

相關問題