简体   繁体   中英

Android app force close

I am new to android world and just started with some examples to gain understanding. In this example I am trying to display image from url but when i start the emulator and click on app icon it force closes.

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 manifest:-

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

at console DDMS i get this error:-

[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)

So how to handle this nullpointer access? Thanks

I would not download the image when the app starts, proberly that causes the exception:

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

Save the image to your 3 res/drawable folders and load it like this, if you want it as a background:

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

Both of the possible locations for NullPointerException I can see are in the line where you set the drawable into the image. They might be either because your image view is null, or because the drawable is null. I suggest you to check if the image view is null before operating it, and check if an exception was thrown in your downloading method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM