getDrawingCache()返回空指针异常 - Cordova插件

时间:2014-11-12 10:01:26

标签: java android cordova phonegap-plugins

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
try {
           Log.i(LOG_TAG, "[Custom toast] toastTitle: toastTitle \n toastText:" );

            Runnable runnable = new Runnable() {
                public void run() {
                    int duration = Toast.LENGTH_SHORT;


                    Context context = cordova.getActivity().getApplicationContext();

                    Toast toast = new Toast(context);
                    toast.setDuration(duration);    

                    LayoutInflater inflater = LayoutInflater.from(context);

                    Resources resources = context.getResources();                       
                    String packageName = context.getPackageName();

                    View appearance = inflater.inflate(resources.getIdentifier("main","layout",packageName),null);
                    toast.setView(appearance);

                    TextView toastTitleView = (TextView) appearance.findViewById(resources.getIdentifier("textView","id",packageName));
                    toastTitleView.setText("Title");

                    toastTitleView.buildDrawingCache();

                    Bitmap bmp = Bitmap.createBitmap(toastTitleView.getDrawingCache());


                    toast.show();
                }
            };
            this.cordova.getActivity().runOnUiThread(runnable);
            callbackContext.success();
            return true;


        callbackContext.error("Invalid action");
        return false;
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
        callbackContext.error(e.getMessage());
        return false;
    }
}

这里我在Cordova插件中创建TextView的位图,但是getDrawingCache()方法返回null ..在做谷歌之后我发现你需要添加setDrawingCacheEnabled(True)但是这也有点帮助..所以请帮助...

1 个答案:

答案 0 :(得分:1)

在设置文本后添加此行

toastTitleView.layout(0, 0, 480, 800);