2

I have an editText, and I want what is being typed in it gets drawn on the canvas which has my resource called "ger". I made a simple code, but In don't get it to work. IDK what is the problem, when I run it, it stops unexpectedly

this is my class code public class StartActivity extends Activity {

/** Called when the activity is first created. */

static Bitmap bmp;
static EditText et;
static ImageView iv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    EditText et = (EditText) findViewById(R.id.editText1);
    ImageView iv = (ImageView) findViewById(R.id.imageView1);
    Bitmap bmp = Bitmap.createBitmap(et.getDrawingCache());
}
public void onDraw (Canvas canvas){

    try {
        canvas.drawColor (Color.BLACK) ;
        Bitmap ab = BitmapFactory.decodeResource(getResources(),(R.drawable.ger)) ;
        ab = bmp;

        canvas.drawBitmap ( ab , 0 , 0 , null ) ;

        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        canvas.drawText(et.toString(),10,10,paint);
        iv.setImageBitmap(bmp);

    } catch ( Exception e ) {
        e.printStackTrace ( ) ;
    } catch ( Error e ) {
        e.printStackTrace ( ) ;
    }

}
}

the logcat:

08-17 22:51:03.526: D/AndroidRuntime(509): Shutting down VM
08-17 22:51:03.526: W/dalvikvm(509): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-17 22:51:03.547: E/AndroidRuntime(509): FATAL EXCEPTION: main
08-17 22:51:03.547: E/AndroidRuntime(509): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.edittext.com/example.edittext.com.StartActivity}: java.lang.NullPointerException
08-17 22:51:03.547: E/AndroidRuntime(509):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-17 22:51:03.547: E/AndroidRuntime(509):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-17 22:51:03.547: E/AndroidRuntime(509):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-17 22:51:03.547: E/AndroidRuntime(509):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-17 22:51:03.547: E/AndroidRuntime(509):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 22:51:03.547: E/AndroidRuntime(509):  at android.os.Looper.loop(Looper.java:123)
08-17 22:51:03.547: E/AndroidRuntime(509):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-17 22:51:03.547: E/AndroidRuntime(509):  at java.lang.reflect.Method.invokeNative(Native Method)
08-17 22:51:03.547: E/AndroidRuntime(509):  at java.lang.reflect.Method.invoke(Method.java:507)
08-17 22:51:03.547: E/AndroidRuntime(509):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-17 22:51:03.547: E/AndroidRuntime(509):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-17 22:51:03.547: E/AndroidRuntime(509):  at dalvik.system.NativeStart.main(Native Method)
08-17 22:51:03.547: E/AndroidRuntime(509): Caused by: java.lang.NullPointerException
08-17 22:51:03.547: E/AndroidRuntime(509):  at android.graphics.Bitmap.createBitmap(Bitmap.java:367)
08-17 22:51:03.547: E/AndroidRuntime(509):  at example.edittext.com.StartActivity.onCreate(StartActivity.java:27)
08-17 22:51:03.547: E/AndroidRuntime(509):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-17 22:51:03.547: E/AndroidRuntime(509):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-17 22:51:03.547: E/AndroidRuntime(509):  ... 11 more

the xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.17"
        android:src="@drawable/ger" />

</LinearLayout>
Simon Sarris
  • 58,131
  • 13
  • 128
  • 161
John Jared
  • 780
  • 3
  • 16
  • 39

1 Answers1

2

The problem here is that you're trying to create a Bitmap from a cache. This can't possibly go well...

What you need to do instead is something like this:

/** Called when the activity is first created. */

static Bitmap bmp;
static EditText et;
static ImageView iv;
static Canvas ivCanvas; // We'll be using our own Canvas.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    EditText et = (EditText) findViewById(R.id.editText1);
    ImageView iv = (ImageView) findViewById(R.id.imageView1);

    // Move this up to onCreate
    Bitmap ab = BitmapFactory.decodeResource(getResources(),(R.drawable.ger)) ;
    bmp = convertToMutable(ab); // Initialize it here with the contents of ab. This effectively clones it and makes it mutable.
    ab = null; // Dispose of ab.

    ivCanvas = new Canvas(bmp); // Create our Canvas!

    // Add a TextWatcher
    et.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            updateCanvas(); // Call the canvas update
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        public void afterTextChanged(Editable s) {
        }
    });
}
public void updateCanvas() {
    ivCanvas.drawColor (Color.BLACK) ;

    ivCanvas.drawBitmap ( bmp , 0 , 0 , null ) ;

    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    ivCanvas.drawText(et.getText().toString(),10,10,paint);

    // Everything has been drawn to bmp, so we can set that here, now.
    iv.setImageBitmap(bmp);

    // Removed the "catch" blocks so you can actually know when you're getting errors! Feel free to re-add later.
}

I've commented this thoroughly so you can understand the changes I've made. I think further changes are necessary here, but this should at least work for you for now.

EDIT:

I've converted the code for use in a TextWatcher. You can find out about basic implementation from this answer.

EDIT 2:

You'll need to convert the Bitmap to a "mutable" (changeable) bitmap. You can use the code found in this answer. I've added a call to that above.

Community
  • 1
  • 1
Eric
  • 63,873
  • 22
  • 120
  • 135
  • Thanks Eric, but I get an error from .onDraw The method onDraw(Canvas) is undefined for the type Activity – John Jared Aug 17 '12 at 21:16
  • Yeah, I kinda' figured you would. I've updated my answer to use a `TextWatcher` implementation. – Eric Aug 17 '12 at 21:22
  • thanks you Eric, but now I'm getting removing the @override annotations error, btw accepted =) – John Jared Aug 17 '12 at 21:33
  • You can safely remove the `TextWatcher`'s `@Override`s if they are giving you errors. – Eric Aug 17 '12 at 21:34
  • and i remove them the app crashes – John Jared Aug 17 '12 at 21:36
  • Can you add the LogCat of that crash to the original post, or put it on [pastebin](http://www.pastebin.com)? – Eric Aug 17 '12 at 21:38
  • I've edited my post again; read the "EDIT2" portion, and you will have to implement that method in order to make the `Bitmap` drawable. – Eric Aug 17 '12 at 21:46
  • I made my bitmap mutabeled, now the app opens but when I write anything on the edittext it don't get typed on the Bitmap ? – John Jared Aug 17 '12 at 21:55
  • Not sure. I feel it may be the `Paint` object. Try using some of the code from [here](https://groups.google.com/forum/?fromgroups#!topic/android-developers/r_LajI1aC5c%5B1-25%5D) where he sets the `Paint` color. – Eric Aug 17 '12 at 22:06