2012-08-17 7 views
2

나는 editText를 가지고 있으며, 입력 된 내용이 "ger"라는 내 리소스가있는 캔버스에 그려지 길 원합니다. 간단한 코드를 만들었지 만 In은 ​​작동하지 않습니다. 나는 그것을 실행할 때 문제가 무엇인지 IDK, 그것은캔버스 위의 Android EditText

가 예기치 않게이 공용 클래스 startActivity를이 활동 확장 내 클래스 코드입니다 중지 {

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

} 
} 

로그 캣 :

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 

는 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> 
+1

LogCat 오류 출력을 게시 할 수 있습니까? 또한,'ger'가 어디에 XML을 보여줄 수 있습니까? – Eric

+0

왜 '오류'를 잡으려고합니까? http://stackoverflow.com/questions/352780/when-to-catch-java-lang-error – slayton

+0

죄송합니다, 업데이트 된 질문을보십시오 .. 아픈 게시 XML now –

답변

2

여기에서 문제는 캐시에서 Bitmap을 만듭니다.

/** 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. 
} 

당신은 내가 변경 한 내용을 이해할 수 있도록 나는 철저하게이 댓글을 달았습니다 : 이것은 아마도 당신이 대신해야 할 일은

이 같은 것입니다 ... 잘되지 수 있습니다. 나는 더 많은 변화가 필요하다고 생각하지만, 적어도 이것은 지금 당신을 위해 일해야합니다.

편집 :

나는 TextWatcher에 사용하기 위해 코드를 변환했습니다. 기본 구현에 대해서는 this answer에서 확인할 수 있습니다.

편집 2 : 당신은 "가변"(변경) 비트 맵에 Bitmap을 변환해야합니다

. this answer에있는 코드를 사용할 수 있습니다. 위의 호출을 추가했습니다.

+0

Thanks Eric,하지만 .onDraw에서 오류가 발생합니다. 액티비티 유형에 대해 onDraw (Canvas) 메서드가 정의되지 않았습니다. –

+0

그래, 좀 이상하게 생각했습니다. 나는'TextWatcher' 구현을 사용하기 위해 나의 대답을 업데이트했습니다. – Eric

+0

에릭에게 감사하지만 지금은 @override 주석 오류를 제거하고 있습니다. btw accepted =) –