android - NullPointerException 安卓崩溃

标签 android nullpointerexception

大家好,我有这个问题: 引起原因:java.lang.NullPointerException 在 com.example.myapplication.Foto$ImageAdapter.getCount(Foto.java:65)//error1 在 android.widget.GridView.setAdapter(GridView.java:182) 在 com.example.myapplication.Foto.onCreate(Foto.java:46)//error2

这是我的代码:

 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.os.Bundle;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.BaseAdapter;
 import android.widget.GridView;
 import android.widget.ImageView;

 import com.example.myapplication.universalimageloader.AbsListViewBaseActivity;
 import com.example.myapplication.universalimageloader.Constants;
 import com.example.myapplication.universalimageloader.ImagePagerActivity;
 import com.nostra13.universalimageloader.core.DisplayImageOptions;

public class Foto extends AbsListViewBaseActivity {


String[] imageUrls;

DisplayImageOptions options;

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

    Bundle bundle = getIntent().getExtras();

    if (bundle != null) {
        imageUrls = bundle.getStringArray(Constants.Extra.IMAGES);
    }

    options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.ic_stub)
            .showImageForEmptyUri(R.drawable.ic_empty)
            .showImageOnFail(R.drawable.ic_error)
            .cacheInMemory()
            .cacheOnDisc()
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    listView = (GridView) findViewById(R.id.gridview);
    ((GridView) listView).setAdapter(new ImageAdapter()); //ERROR 2
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startImagePagerActivity(position);
        }
    });
}

private void startImagePagerActivity(int position) {
    Intent intent = new Intent(this, ImagePagerActivity.class);
    intent.putExtra(Constants.Extra.IMAGES, imageUrls);
    intent.putExtra(Constants.Extra.IMAGE_POSITION, position);
    startActivity(intent);
}

public class ImageAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return imageUrls.length;  //ERROR 1
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ImageView imageView;
        if (convertView == null) {
            imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
        } else {
            imageView = (ImageView) convertView;
        }

        imageLoader.displayImage(imageUrls[position], imageView, options);

        return imageView;
    }
}
}

我能做什么?

public final class Constants {

public static final String[] IMAGES = new String[] {
        // Heavy images
        "https://lh6.googleusercontent.com/-jZgveEqb6pg/T3R4kXScycI/AAAAAAAAAE0/xQ7CvpfXDzc/s1024/sample_image_01.jpg",
        "https://lh4.googleusercontent.com/-K2FMuOozxU0/T3R4lRAiBTI/AAAAAAAAAE8/a3Eh9JvnnzI/s1024/sample_image_02.jpg",
        "https://lh5.googleusercontent.com/-SCS5C646rxM/T3R4l7QB6xI/AAAAAAAAAFE/xLcuVv3CUyA/s1024/sample_image_03.jpg",
        "https://lh6.googleusercontent.com/-f0NJR6-_Thg/T3R4mNex2wI/AAAAAAAAAFI/45oug4VE8MI/s1024/sample_image_04.jpg",
        "https://lh3.googleusercontent.com/-n-xcJmiI0pg/T3R4mkSchHI/AAAAAAAAAFU/EoiNNb7kk3A/s1024/sample_image_05.jpg",
        "https://lh3.googleusercontent.com/-X43vAudm7f4/T3R4nGSChJI/AAAAAAAAAFk/3bna6D-2EE8/s1024/sample_image_06.jpg",
        "https://lh5.googleusercontent.com/-MpZneqIyjXU/T3R4nuGO1aI/AAAAAAAAAFg/r09OPjLx1ZY/s1024/sample_image_07.jpg",
        "https://lh6.googleusercontent.com/-ql3YNfdClJo/T3XvW9apmFI/AAAAAAAAAL4/_6HFDzbahc4/s1024/sample_image_08.jpg",
        "https://lh5.googleusercontent.com/-Pxa7eqF4cyc/T3R4oasvPEI/AAAAAAAAAF0/-uYDH92h8LA/s1024/sample_image_09.jpg",
        "https://lh4.googleusercontent.com/-Li-rjhFEuaI/T3R4o-VUl4I/AAAAAAAAAF8/5E5XdMnP1oE/s1024/sample_image_10.jpg",
        "https://lh5.googleusercontent.com/-_HU4fImgFhA/T3R4pPVIwWI/AAAAAAAAAGA/0RfK_Vkgth4/s1024/sample_image_11.jpg",
        "https://lh6.googleusercontent.com/-0gnNrVjwa0Y/T3R4peGYJwI/AAAAAAAAAGU/uX_9wvRPM9I/s1024/sample_image_12.jpg",
        "https://lh3.googleusercontent.com/-HBxuzALS_Zs/T3R4qERykaI/AAAAAAAAAGQ/_qQ16FaZ1q0/s1024/sample_image_13.jpg",
        "https://lh4.googleusercontent.com/-cKojDrARNjQ/T3R4qfWSGPI/AAAAAAAAAGY/MR5dnbNaPyY/s1024/sample_image_14.jpg",
        "https://lh3.googleusercontent.com/-WujkdYfcyZ8/T3R4qrIMGUI/AAAAAAAAAGk/277LIdgvnjg/s1024/sample_image_15.jpg",
        "https://lh6.googleusercontent.com/-FMHR7Vy3PgI/T3R4rOXlEKI/AAAAAAAAAGs/VeXrDNDBkaw/s1024/sample_image_16.jpg",
        "https://lh4.googleusercontent.com/-mrR0AJyNTH0/T3R4rZs6CuI/AAAAAAAAAG0/UE1wQqCOqLA/s1024/sample_image_17.jpg",
        "https://lh6.googleusercontent.com/-z77w0eh3cow/T3R4rnLn05I/AAAAAAAAAG4/BaerfWoNucU/s1024/sample_image_18.jpg",
        "https://lh5.googleusercontent.com/-aWVwh1OU5Bk/T3R4sAWw0yI/AAAAAAAAAHE/4_KAvJttFwA/s1024/sample_image_19.jpg",
        "https://lh6.googleusercontent.com/-q-js52DMnWQ/T3R4tZhY2sI/AAAAAAAAAHM/A8kjp2Ivdqg/s1024/sample_image_20.jpg",
        "https://lh5.googleusercontent.com/-_jIzvvzXKn4/T3R4t7xpdVI/AAAAAAAAAHU/7QC6eZ10jgs/s1024/sample_image_21.jpg",
        "https://lh3.googleusercontent.com/-lnGi4IMLpwU/T3R4uCMa7vI/AAAAAAAAAHc/1zgzzz6qTpk/s1024/sample_image_22.jpg",
        "https://lh5.googleusercontent.com/-fFCzKjFPsPc/T3R4u0SZPFI/AAAAAAAAAHk/sbgjzrktOK0/s1024/sample_image_23.jpg",
        "https://lh4.googleusercontent.com/-8TqoW5gBE_Y/T3R4vBS3NPI/AAAAAAAAAHs/EZYvpNsaNXk/s1024/sample_image_24.jpg",
        "https://lh6.googleusercontent.com/-gc4eQ3ySdzs/T3R4vafoA7I/AAAAAAAAAH4/yKii5P6tqDE/s1024/sample_image_25.jpg",
        "https://lh5.googleusercontent.com/--NYOPCylU7Q/T3R4vjAiWkI/AAAAAAAAAH8/IPNx5q3ptRA/s1024/sample_image_26.jpg",
        "https://lh6.googleusercontent.com/-9IJM8so4vCI/T3R4vwJO2yI/AAAAAAAAAIE/ljlr-cwuqZM/s1024/sample_image_27.jpg",
        "https://lh4.googleusercontent.com/-KW6QwOHfhBs/T3R4w0RsQiI/AAAAAAAAAIM/uEFLVgHPFCk/s1024/sample_image_28.jpg",
        "https://lh4.googleusercontent.com/-z2557Ec1ctY/T3R4x3QA2hI/AAAAAAAAAIk/9-GzPL1lTWE/s1024/sample_image_29.jpg",
        "https://lh5.googleusercontent.com/-LaKXAn4Kr1c/T3R4yc5b4lI/AAAAAAAAAIY/fMgcOVQfmD0/s1024/sample_image_30.jpg",
        "https://lh4.googleusercontent.com/-F9LRToJoQdo/T3R4yrLtyQI/AAAAAAAAAIg/ri9uUCWuRmo/s1024/sample_image_31.jpg",
        "https://lh4.googleusercontent.com/-6X-xBwP-QpI/T3R4zGVboII/AAAAAAAAAIs/zYH4PjjngY0/s1024/sample_image_32.jpg",
        "https://lh5.googleusercontent.com/-VdLRjbW4LAs/T3R4zXu3gUI/AAAAAAAAAIw/9aFp9t7mCPg/s1024/sample_image_33.jpg",
        "https://lh6.googleusercontent.com/-gL6R17_fDJU/T3R4zpIXGjI/AAAAAAAAAI8/Q2Vjx-L9X20/s1024/sample_image_34.jpg",
        "https://lh3.googleusercontent.com/-1fGH4YJXEzo/T3R40Y1B7KI/AAAAAAAAAJE/MnTsa77g-nk/s1024/sample_image_35.jpg",
        "https://lh4.googleusercontent.com/-Ql0jHSrea-A/T3R403mUfFI/AAAAAAAAAJM/qzI4SkcH9tY/s1024/sample_image_36.jpg",
        "https://lh5.googleusercontent.com/-BL5FIBR_tzI/T3R41DA0AKI/AAAAAAAAAJk/GZfeeb-SLM0/s1024/sample_image_37.jpg",
        "https://lh4.googleusercontent.com/-wF2Vc9YDutw/T3R41fR2BCI/AAAAAAAAAJc/JdU1sHdMRAk/s1024/sample_image_38.jpg",
        "https://lh6.googleusercontent.com/-ZWHiPehwjTI/T3R41zuaKCI/AAAAAAAAAJg/hR3QJ1v3REg/s1024/sample_image_39.jpg",
        // Light images
        "http://tabletpcssource.com/wp-content/uploads/2011/05/android-logo.png",
        "http://simpozia.com/pages/images/stories/windows-icon.png",
        "https://si0.twimg.com/profile_images/1135218951/gmail_profile_icon3_normal.png",
        "http://www.krify.net/wp-content/uploads/2011/09/Macromedia_Flash_dock_icon.png",
        "http://radiotray.sourceforge.net/radio.png",
        "http://www.bandwidthblog.com/wp-content/uploads/2011/11/twitter-logo.png",
        "http://weloveicons.s3.amazonaws.com/icons/100907_itunes1.png",
        "http://weloveicons.s3.amazonaws.com/icons/100929_applications.png",
        "http://www.idyllicmusic.com/index_files/get_apple-iphone.png",
        "http://www.frenchrevolutionfood.com/wp-content/uploads/2009/04/Twitter-Bird.png",
        "http://3.bp.blogspot.com/-ka5MiRGJ_S4/TdD9OoF6bmI/AAAAAAAAE8k/7ydKtptUtSg/s1600/Google_Sky%2BMaps_Android.png",
        "http://www.desiredsoft.com/images/icon_webhosting.png",
        "http://goodereader.com/apps/wp-content/uploads/downloads/thumbnails/2012/01/hi-256-0-99dda8c730196ab93c67f0659d5b8489abdeb977.png",
        "http://1.bp.blogspot.com/-mlaJ4p_3rBU/TdD9OWxN8II/AAAAAAAAE8U/xyynWwr3_4Q/s1600/antivitus_free.png",
        "http://cdn3.iconfinder.com/data/icons/transformers/computer.png",
        "http://cdn.geekwire.com/wp-content/uploads/2011/04/firefox.png?7794fe",
        "https://ssl.gstatic.com/android/market/com.rovio.angrybirdsseasons/hi-256-9-347dae230614238a639d21508ae492302340b2ba",
        "http://androidblaze.com/wp-content/uploads/2011/12/tablet-pc-256x256.jpg",
        "http://www.theblaze.com/wp-content/uploads/2011/08/Apple.png",
        "http://1.bp.blogspot.com/-y-HQwQ4Kuu0/TdD9_iKIY7I/AAAAAAAAE88/3G4xiclDZD0/s1600/Twitter_Android.png",
        "http://3.bp.blogspot.com/-nAf4IMJGpc8/TdD9OGNUHHI/AAAAAAAAE8E/VM9yU_lIgZ4/s1600/Adobe%2BReader_Android.png",
        "http://cdn.geekwire.com/wp-content/uploads/2011/05/oovoo-android.png?7794fe",
        "http://icons.iconarchive.com/icons/kocco/ndroid/128/android-market-2-icon.png",
        "http://thecustomizewindows.com/wp-content/uploads/2011/11/Nicest-Android-Live-Wallpapers.png",
        "http://c.wrzuta.pl/wm16596/a32f1a47002ab3a949afeb4f",
        "http://macprovid.vo.llnwd.net/o43/hub/media/1090/6882/01_headline_Muse.jpg",
        // Special cases
        "file:///sdcard/Universal Image Loader @#&=+-_.,!()~'%20.png", // Image from SD card with encoded symbols
        "assets://Living Things @#&=+-_.,!()~'%20.jpg", // Image from assets
        "drawable://" + R.drawable.ic_launcher, // Image from drawables

        "https://www.eff.org/sites/default/files/chrome150_0.jpg", // Image from HTTPS
        "http://bit.ly/soBiXr", // Redirect link
        "http://img001.us.expono.com/100001/100001-1bc30-2d736f_m.jpg", // EXIF
        "", // Empty link
        "http://wrong.site.com/corruptedLink", // Wrong link
};

private Constants() {
}

public static class Config {
    public static final boolean DEVELOPER_MODE = false;
}

public static class Extra {
    public static final String IMAGES = "com.example.myapplication.universalimageloader.IMAGES";
    public static final String IMAGE_POSITION = "com.example.myapplication.universalimageloader.IMAGE_POSITION";
}
}

最佳答案

imageUrls 仅在有包数据时才初始化,如果没有,则在设置适配器时数据为空。您需要做的是在尝试访问属性之前确保(或检查)imageUrls 是否为 null(例如在 imageUrls.length 调用中)。

更具体地说,这些行:

if (bundle != null) {
   imageUrls = bundle.getStringArray(Constants.Extra.IMAGES);
}

将初始化 imageUrls 数组仅当 bundle 不为 null 时,但如果它是......它会中断。

关于android - NullPointerException 安卓崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17265878/

相关文章:

安卓, NetworkInfo.getTypeName(), NullpointerException

android - 与 FragmentManager.popBackStack 相关的 NullPointerException。如何解决?

java - 类测试期间出现 NullPointerException

java - 在类中找不到 Social_Security_Number 的 setter /字段

android - 在 motionevent 中找不到 settooltype 方法

Android 无法识别变体 arm-debug 和设备的 apk

android - Zygote 共享库处理

java - Nexus 6P 上硬件传感器的 Android 采样率变化

android - LinkMovementMethod 不滚动 TextView

android - 静态单例未在 Activity 中初始化。有什么问题吗?