java.lang.NullPointerException:尝试在空对象引用

时间:2016-05-18 06:40:28

标签: java android android-studio

我是Android开发新手,因此感谢实际代码。提前谢谢。

这是我的代码。我使用Viewpager

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        BitmapDrawable btmpdraw = (BitmapDrawable) imgflag.getDrawable();
        Bitmap btmp = btmpdraw.getBitmap();

        File cache = getApplicationContext().getExternalCacheDir();
        File sharefile = new File(cache, "toshare.png");
        try {
            FileOutputStream out = new FileOutputStream(sharefile);
            btmp.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (IOException e) {

        }

这是我的logcat:

E / AndroidRuntime:致命异常:主要                                                                            过程:kokamapps.viewpagertutorial,PID:2049                                                                             java.lang.NullPointerException:尝试调用虚方法' android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()'在空对象引用                                                                                在kokamapps.viewpagertutorial.MainActivity.onOptionsItemSelected(MainActivity.java:75)                                                                                在android.app.Activity.onMenuItemSelected(Activity.java:2967)                                                                                在android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)                                                                                在android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)                                                                                在android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)                                                                                在android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:663)                                                                                在android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)                                                                                在android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)                                                                                在android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)                                                                                在android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)                                                                                在android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)                                                                                在android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)                                                                                在android.view.View.performClick(View.java:4794)                                                                                在android.view.View $ PerformClick.run(View.java:19905)                                                                                在android.os.Handler.handleCallback(Handler.java:739)                                                                                在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                在android.os.Looper.loop(Looper.java:139)                                                                                在android.app.ActivityThread.main(ActivityThread.java:5298)                                                                                at java.lang.reflect.Method.invoke(Native Method)                                                                                在java.lang.reflect.Method.invoke(Method.java:372)                                                                                在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:950)                                                                                在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

0 个答案:

没有答案