尝试调用虚方法' void android.widget.GridView.setAdapter(android.widget.ListAdapter)'在null对象引用上 - Android Activity

时间:2017-03-15 06:34:14

标签: android-adapter android-gridview

我一直在尝试开发一个应用程序,点击图像按钮,它使用位图绘制整个屏幕并将其保存在目录中,并通过传递意图显示该目录中所有图像的完整列表。 ...上面两个步骤只需单击即可完成,我可以将位图保存在指定目录中,但无法在自定义网格视图库中检索图像...发布我写的代码

公共类共享扩展SecureActivity {

private int count;
Context context;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter = null;
ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;
GridView imageGrid;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getFromSdcard();
    imageGrid = (GridView) findViewById(R.id.imageGrid);
    imageAdapter = new ImageAdapter();
    imageGrid.setAdapter(imageAdapter);

}

public void getFromSdcard()
{
    File file= new File(android.os.Environment.getExternalStorageDirectory(),"ScreenShots");

    if (file.isDirectory())
    {
        listFile = file.listFiles();

        for (int i = 0; i < listFile.length; i++)
        {

            f.add(listFile[i].getAbsolutePath());

        }
    }
    String size = String.valueOf(f.size());
    String name = f.get(f.size()-1);
   /*// ImageView imagegrid = (ImageView) findViewById(R.id.PhoneImageGrid);
    try{

        Bitmap bm = BitmapFactory.decodeFile(name);
        bm.prepareToDraw();
        imagegrid.setImageBitmap(bm);

    }

    catch(Exception e)
    {
        e.printStackTrace();
    }

    Log.d(size,"number of files");
    Log.d(name,"image file");

* /     }

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return f.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.gallery_item, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }


        Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
        holder.imageview.setImageBitmap(myBitmap);
        return convertView;
    }
}
class ViewHolder {
    ImageView imageview;


}

有了这个,我收到了以下错误

FATAL EXCEPTION: main

                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edutor.ignitor/com.edutor.ignitor.EdutorShare}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2556)
                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:170)
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                  at android.os.Looper.loop(Looper.java:194)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5568)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
                                                                  at com.edutor.ignitor.EdutorShare.onCreate(EdutorShare.java:43)
                                                                  at android.app.Activity.performCreate(Activity.java:6041)
                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2556) 
                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:170) 
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                  at android.os.Looper.loop(Looper.java:194) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5568) 
                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750) 

有人可以帮忙......我哪里出错了

0 个答案:

没有答案