2012-02-06 63 views
0

我用這個從Android開發人員,但不明白爲什麼它強制關閉:掙扎創建一個簡單的形狀

包com.example.shapedrawable.CustomDrawableView;

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.drawable.ShapeDrawable; 
import android.graphics.drawable.shapes.OvalShape; 
import android.view.View; 

public class CustomDrawableViewActivity extends View { 
    private ShapeDrawable mDrawable; 

    public CustomDrawableViewActivity(Context context) { 
    super(context); 

    int x = 10; 
    int y = 10; 
    int width = 300; 
    int height = 50; 

    mDrawable = new ShapeDrawable(new OvalShape()); 
    mDrawable.getPaint().setColor(Color.BLUE); 
    mDrawable.setBounds(x, y, x + width, y + height); 
    } 

    protected void onDraw(Canvas canvas) { 
    mDrawable.draw(canvas); 
    } 
    } 
+0

你需要點擊DDMS按鈕並查看LogCat。通過點擊小E按鈕來過濾消息,然後運行你的應用程序。它應該告訴你錯誤是什麼。 – Lumis 2012-02-06 23:24:50

+0

謝謝我現在看到它 – user1169775 2012-02-08 12:53:14

回答

1

你不說其中它強行關閉這始終是有用的信息,但我以爲這是在這一行:直到調用setPaint()

mDrawable.getPaint().setColor(Color.BLUE); 

getPaint()將返回null。試試這個:

Paint paint = new Paint(); 
paint.setColor(Color.BLUE); 
mDrawable.setPaint(paint); 
+0

謝謝,但它仍然強制關閉,老實說,我不知道它會出錯哪裏 – user1169775 2012-02-06 23:18:01