2016-11-06 80 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。