2013-07-18 89 views
2

如果我想在此類中添加一個按鈕,以便我可以調用onclicklistener,我應該怎麼做?將視圖添加到活動

public class GameView extends View { 
    Path circle; 
    Paint cPaint; 
    Paint tPaint; 
    String z; 
    int i = 65, strt, arc, leftx, topy, rightx, bottomy, maxx, maxy; 
    boolean flag1, flag2, flag3; 
    double n1, n2; 
    int n, n3 = 180,n4,n5 = 90; 
    float f1 = 180, f2 = 90; 
    Button b1; 
    Random r = new Random(); 
    RectF oval; 

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

     leftx = 0; 
     topy = 60; 
     rightx = 150; 
     bottomy = 120; 

     z = String.valueOf(Character.toChars(i)); 

     cPaint = new Paint(); 
     cPaint.setColor(Color.RED); 

     strt = 45; 
     arc = 315; 

     n1 = Math.random() * 600; 
     Log.d("random", z); 

     if (flag2 == false) 
      new DrawThread(this); 

     // cPaint.setStrokeWidth(2); 
     tPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     tPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
     tPaint.setColor(Color.BLACK); 
     float scale = getResources().getDisplayMetrics().scaledDensity; 
     tPaint.setTextSize(20 * scale); 
    } 

    public void onSizeChanged(int w,int h,int oldh,int oldw) { 
     maxx = oldw; 
     maxy = oldh; 
    } 

    //@Override 
    protected void onDraw(Canvas canvas) { 
     // Drawing commands go here 
     oval = new RectF(leftx,topy,rightx,bottomy); 
     canvas.drawArc(oval, strt, arc, true, cPaint); 
     while (i < 90) { 
      canvas.drawText(String.valueOf(Character.toChars(i)),f1,f2, tPaint); 
      break; 
     } 
    } 
} 

回答

2
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout); 

添加上述下方setContentView

setContentView(R.layout.activity_new_game); 
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout); 

你需要使用setContentView設置佈局,然後初始化意見。

注意可以設置爲活動的當前視圖層次的findViewById

編輯:

實施例:這是一個例子。我不明白爲什麼它不起作用,如果你做類似如下。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".NewGame" 
android:id="@+id/relative_layout" 
> 

<Button 
    android:id="@+id/prev_button" 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/next_button" 
    android:layout_alignBottom="@+id/next_button" 
    android:layout_centerHorizontal="true" 
    android:text="stop_label" /> 

    <Button 
    android:id="@+id/next_button" 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_marginRight="20dp" 
    android:layout_toLeftOf="@+id/prev_button" 
    android:text="start_label" /> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:id="@+id/rl" 
     android:layout_below="@+id/prev_button" 
     android:layout_height="wrap_content" 

     > 
    </RelativeLayout> 

</RelativeLayout> 

活動

public class MainActivity extends Activity { 
    private Button btn1; 
    private EditText edt1, edt2, edt3, edt4 ,edt5, edt6; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     MyView mv = new MyView(this); 
     setContentView(R.layout.activity_main); 
     RelativeLayout layout= (RelativeLayout) findViewById(R.id.rl); 
     layout.addView(mv); 
    } 
    class MyView extends View 
    { 

     public MyView(Context context) { 
      super(context); 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      // TODO Auto-generated method stub 
      super.onDraw(canvas); 
      canvas.drawColor(Color.RED); 
     } 

    } 
} 

快照

enter image description here

+0

現在不給任何錯誤,但內容後初始化意見的遊戲視圖沒有在屏幕上顯示 – user2586942

+0

@ user2586942向我們展示gameview.You應該得到NPE coz初始化相對佈局將失敗。 – Raghunandan

+0

先生的遊戲視角與我之前發佈的問題相同。 – user2586942

0
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout); 

你不能setContentView之前調用findViewById。移動

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout); 

setContentView(R.layout.activity_new_game);

1

你需要試着找相對佈局之前,虛增您的觀點。嘗試:

RelativeLayout layout; 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    gameview=new GameView(this); 
    setContentView(R.layout.activity_new_game); 
    layout = (RelativeLayout) findViewById(R.id.relative_layout); 

    //LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    //view = mInflater.inflate(R.layout.activity_new_game, gameview, true); 

    layout.addView(gameview); 

} 
0

你沒有在你的代碼初始化layout所以首先初始化layoutsetContentView後,再加入以您的layout

RelativeLayout layout; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_new_game); 
    //Initialize game view 
    GameView gameview=new GameView(this); 
    //initialize layout 
    layout = (RelativeLayout) findViewById(R.id.relative_layout); 
    //adding game view to layout 
    layout.addView(gameview); 
} 

編輯:

初始化這樣setContentView之前

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout); 

是錯誤的做法,你只能setContentView像上面的代碼