2017-02-17 37 views
0

我是編程的初學者...所以也許這個問題聽起來很有趣。 我在MainActivity內部創建一個新的CanvasView時遇到了問題,因爲它在其括號內寫了一些東西。 這裏是我的MainActivity:創建新的CanvasView

public class MainActivity extends AppCompatActivity { 
    CanvasView myView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     //the line below doesnt work 
     myView = new CanvasView(); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 
    public void button1(View v){ 
     CanvasView.l = 400; 
     myView.invalidate(); 
    } 
} 

這裏是我CanvasView:

public class CanvasView extends View{ 
    Paint paint = new Paint(); 
    static int l = 500; 

    public CanvasView (Context context, AttributeSet attrs) { 
     super(context); 
     paint = new Paint(); 
     paint.setAntiAlias(true); 
     paint.setColor(Color.GREEN); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeWidth(10); 
    } 
    @Override 
    public void onDraw(final Canvas canvas) { 
     canvas.drawLine(100,100,l,100,paint); 
    } 
} 

回答

0

super.onCreate後創建YOUT帆布的setContentView像下面

super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myView = new CanvasView();

+0

我真的不明白是什麼你的意思是你的第一行:「super.onCreate方法。」 – nok4