Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 设置imageBitmap时出现空指针异常_Android_Nullpointerexception - Fatal编程技术网

Android 设置imageBitmap时出现空指针异常

Android 设置imageBitmap时出现空指针异常,android,nullpointerexception,Android,Nullpointerexception,我这里有一个复选框的onClick方法: @Override public void onClick(View v) { if (addCheckbox.isChecked()) { System.out.println("Checked"); PackageManager pm = mContext.getPackageManager(); final int DEST_IMAGE_WIDTH = 100; fina

我这里有一个复选框的onClick方法:

@Override
public void onClick(View v) {
    if (addCheckbox.isChecked()) {
        System.out.println("Checked");

        PackageManager pm = mContext.getPackageManager();
        final int   DEST_IMAGE_WIDTH = 100;
        final int DEST_IMAGE_HEIGHT = 100;
        ApplicationInfo appInfo = mContext.getApplicationInfo();
        Drawable appIcon = pm.getApplicationIcon(appInfo);
        Bitmap appBmp  = Bitmap.createBitmap(DEST_IMAGE_WIDTH, DEST_IMAGE_HEIGHT, Config.ARGB_8888); 

        // Creates a new canvas based on the image specification
        // created just above.
        Canvas canvas = new Canvas(appBmp);
        // (optional) Fills the entire canvas
        canvas.drawColor(Color.WHITE);
        // You need to set bounds otherwise a 0,0 sized image would be drawn.
        appIcon.setBounds(0, 0, DEST_IMAGE_WIDTH, DEST_IMAGE_HEIGHT);
        appIcon.draw(canvas);

        /// Let's save to a .jpg file ...
        File file = new File(mContext.getFilesDir().getAbsolutePath() + "/test2.jpg");
        FileOutputStream out;
        try
        {
            file.createNewFile();
            out = mContext.getApplicationContext().openFileOutput("BitmapImage", Context.MODE_PRIVATE);
            appBmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
            Log.i("AppInfoAdapter", "the icon(s) have been saved");
            out.close();

            // Load back the image file to confirm it works
            // Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath() );
            // ImageView imageV = (ImageView)findViewById(R.id.);
            // imageV.setImageBitmap(bitmap);
        }
        catch (FileNotFoundException e1)
        {
            e1.printStackTrace();
        }
        catch (IOException e2)
        {
            e2.printStackTrace();
        }

        Intent intent = new Intent (v.getContext(), GVABackup.class);
        v.getContext().startActivity(intent);
        Log.i("AppInfoAdapter", "New intent started to send icon bitmap");

    } else {
        System.out.println("Un-Checked");
    }
}
然后GVABackup.class是:

package com.example.awesomefilebuilderwidget;
IMPORTS
public class GVABackup extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    ImageView view = (ImageView)findViewById(R.id.layout1);

    Log.i("GridViewAdapter", "checkbox is checked");
    FileInputStream in = null;
    try {
        in = openFileInput("BitmapImage");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Load back the image file to confirm it works
    Bitmap bitmap = BitmapFactory.decodeStream(in);
    view.setImageBitmap(bitmap);
    try {
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}}
另外请注意,我的gridView适配器中有这个类,因此我可以从存储器中获取位图,然后将其设置为imageView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    boolean checked = (mCheckBox==null)?false:(((CheckBox)  mCheckBox).isChecked());
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(Context);
        Log.d("GridViewAdapter", "new imageView added");
    }
    if(checked == true){
        isSdReadable();
        Intent intent = new Intent (view.getContext(), GVABackup.class);
           view.getContext().startActivity(intent);
    } else {
        Log.e("GridView", "Icons not for use/checkbox not checked");
    }
    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;
}
但当我运行这段代码时,我得到了这个NPE:

12-14 16:04:51.197: I/System.out(3245): Checked
12-14 16:04:51.227: I/AppInfoAdapter(3245): the icon(s) have been saved
12-14 16:04:51.237: I/AppInfoAdapter(3245): New intent started to send icon bitmap
12-14 16:04:51.307: I/GridViewAdapter(3245): checkbox is checked
12-14 16:04:51.317: D/AndroidRuntime(3245): Shutting down VM
12-14 16:04:51.317: W/dalvikvm(3245): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
12-14 16:04:51.347: E/AndroidRuntime(3245): FATAL EXCEPTION: main
12-14 16:04:51.347: E/AndroidRuntime(3245): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.GVABackup}: java.lang.NullPointerException
12-14 16:04:51.347: E/AndroidRuntime(3245):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1833)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1854)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at android.os.Looper.loop(Looper.java:150)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at android.app.ActivityThread.main(ActivityThread.java:4333)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at java.lang.reflect.Method.invokeNative(Native Method)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at java.lang.reflect.Method.invoke(Method.java:507)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at dalvik.system.NativeStart.main(Native Method)
12-14 16:04:51.347: E/AndroidRuntime(3245): Caused by: java.lang.NullPointerException
12-14 16:04:51.347: E/AndroidRuntime(3245):     at com.example.awesomefilebuilderwidget.GVABackup.onCreate(GVABackup.java:33)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
12-14 16:04:51.347: E/AndroidRuntime(3245):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1797)
12-14 16:04:51.347: E/AndroidRuntime(3245):     ... 11 more
这是第33行:

view.setImageBitmap(bitmap);
所以很明显,位图返回null,这意味着我的FileInputStream没有得到任何东西

我怎样才能解决这个问题


请注意,layout1 imageView应该是来自my GridViewAdapter的视图imageView

您必须将setContentViewlayoutFileName.xml放在super.onCreatesavedInstanceState之后;行,NPE发生的原因是没有加载imageView,因为没有引用此活动的xml文件。

否,如果是第33行,则视图为null。好的,那么如何确保视图不会返回null?我仍然获得NPE,另外,我使用的imageView并不是来自布局文件,而是在gridView适配器的getView部分动态创建的,即名为view的布局,它的id为layout1@user2909006:imageView我使用的不是布局文件,而是在gridView适配器的getView部分动态创建的-否,事实并非如此。您正在活动中调用findViewById,而不是适配器。如果要在活动中调用findViewById,需要首先调用setContentView。