2016-11-06 81 views
1

我试图在位图上写文本,我需要edittex才能检查键盘输入。 我找到平局的EditText此代码在画布上,但它返回null:无法在画布上绘制EditText

Attempt to invoke virtual method 'void android.widget.EditText.setDrawingCacheEnabled(boolean)' on a null object reference 

编辑:满级


public class TextMenu { 
private final Toolbox toolbox; 
private final AppPanel appPanel; 
private final Context context; 
private final MainActivity mainActivity; 
private final Resources resources; 
private Bitmap textTool, Click, textPlace; 
private final float xscaleFactor, yscaleFactor; 
private boolean allowDraw; 
private EditText editText; 

public TextMenu(Context context, Resources resources, float xscaleFactor, float yscaleFactor, Toolbox toolbox, AppPanel appPanel, MainActivity mainActivity) { 
    this.xscaleFactor = xscaleFactor; 
    this.yscaleFactor = yscaleFactor; 
    this.resources = resources; 
    this.toolbox = toolbox; 
    this.appPanel = appPanel; 
    this.context = context; 
    this.mainActivity = mainActivity; 

    textPlace = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(resources, R.drawable.text_place), (int) (xscaleFactor * 93), (int) (yscaleFactor * 193), true); 
    textTool = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(resources, R.drawable.text_menu), (int) (xscaleFactor * 480), (int) (yscaleFactor * 100), true); 
    Click = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(resources, R.drawable.tools_click), (int) (xscaleFactor * 80), (int) (yscaleFactor * 80), true); 

    editText = new EditText(context); 
    editText.setText("My Text"); 
    editText.setWidth(180); 
    editText.setBackgroundColor(Color.WHITE); 

} 

public void draw(Canvas canvas) { 
    if (allowDraw) { 
     canvas.drawBitmap(textTool, 0, screenH - textTool.getHeight(), null); 
     command(canvas); 
     editText.setDrawingCacheEnabled(true); 
//   editText.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), 
//     View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
      editText.buildDrawingCache(true); 
      Bitmap b = editText.getDrawingCache(); 
      canvas.drawBitmap(b, 100, 100, null); 
      // editText.draw(canvas); 
      mainActivity.getKeyboard(); 
     } 
    } 
} 

我该怎么办呢?

+0

它编译?或者错误是运行时错误? – ItamarG3

+0

您能否提供完整的源代码? –

+0

@ItamarGreen当我打电话给我的班时,我的应用程序崩溃 – MREZA

回答

0

我认为在系统调用绘图方法后,您正在创建editText对象。 确保在自定义视图的构造函数中实例化editText。