2016-11-06 4 views
1

Ich versuche, Text auf einer Bitmap zu schreiben, und ich brauche ein Edittex, um Eingaben von der Tastatur zu überprüfen. finde ich diesen Code für Unentschieden EditText auf der Leinwand, aber es null zurück:nicht zeichnen EditText auf Canvas

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

Edit: Voll Klasse


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(); 
     } 
    } 
} 

wie kann ich das tun?

+0

kompiliert es? oder ist der Fehler ein Laufzeitfehler? – ItamarG3

+0

Könnten Sie bitte vollständigen Quellcode bereitstellen? –

+0

@ItamarGreen, wenn ich seine Klasse anrufen meine App stürzt – MREZA

Antwort

0

Ich denke, dass Sie Ihr editText-Objekt erstellen, nachdem das System Ihre Zeichenmethode aufruft. Stellen Sie sicher, dass editText im Konstruktor Ihrer benutzerdefinierten Ansicht instanziiert wird.

Verwandte Themen