Android app力量关闭

时间:2011-10-23 19:48:43

标签: java android

我是android世界的新手,刚开始用一些例子来获得理解。在这个例子中,我试图从url显示图像,但是当我启动模拟器并单击应用程序图标时,它会强制关闭。

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)

那么如何处理这个nullpointer访问?感谢

2 个答案:

答案 0 :(得分:1)

我不会在应用程序启动时下载图像,因此会导致异常:

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

如果您想将图像作为背景,请将图像保存到3个res / drawable文件夹并像这样加载:

<?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>

答案 1 :(得分:0)

我可以看到NullPointerException的两个可能位置都在您将drawable设置为图像的行中。它们可能是因为您的图像视图为null,或者因为drawable为null。我建议您在操作之前检查图像视图是否为空,并检查下载方法中是否抛出了异常。