2013-07-20 67 views
0

這是我的擴展視圖:如何將畫布添加到Android中的擴展視圖?

public class PieMenu extends FrameLayout{ 



    private View _view; 
    private ArrayList<PieItem> mItems; 
    private Context _context; 


    public PieMenu(View view, Context context) { 
     super(context); 
     _context = context; 
     _view = view; 
     setWillNotDraw(false); 
     // TODO Auto-generated constructor stub 
    } 



    public void addPieMenu(int x, int y){ 

     mItems = new ArrayList<PieItem>(); 
     Path path = new Path(); 
     path.addCircle(9, 9, 9, Path.Direction.CW); 
     PieItem item = new PieItem(_view,_context,path, x,y,40); 
     mItems.add(item); 
     //FrameLayout.LayoutParams lyp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); 
     //pieView.setLayoutParams(lyp); 
     //addView(pieView); 
     // animateIn(); 

     invalidate(); 

    } 



     @Override 
     protected void onDraw(Canvas canvas) { 

     Paint paint1 = new Paint(); 
     paint1.setColor(Color.RED); 
     canvas.drawCircle(50, 50, 25, paint1); 

      this.draw(canvas); 
      //_view.draw(canvas); 

     } 




} 

這是我的主要活動:

public class MainActivity extends Activity { 
    PieMenu pieMenu; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     pieMenu = new PieMenu(getWindow().getDecorView().findViewById(android.R.id.content) ,MainActivity.this); 


     FrameLayout fl = (FrameLayout)findViewById(R.id.flayout); 
     fl.addView(pieMenu); 


    } 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     int x = (int)event.getX(); 
     int y = (int)event.getY(); 
     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
      { 
       pieMenu.addPieMenu(x,y); 
      } 
      case MotionEvent.ACTION_MOVE: 
      case MotionEvent.ACTION_UP: 
     } 
    return false; 
    } 

} 

這是我的主要activity.xml:

<FrameLayout 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:id="@+id/flayout" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="@string/hello_world" /> 

    <com.example.piemenu.PieMenu 
     android:id="@+id/circle_view" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerInParent="true" /> 

</FrameLayout> 

當我編譯此,應用程序與此錯誤崩潰:

07-20 21:38:45.906: E/AndroidRuntime(10088): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] 

我哪裏錯了?

+0

上線就錯誤發生的? – Raghunandan

回答

2

您將需要這些構造在PieMenu類:

public PieMenu(View view, Context context) { 
    super(context); 
    // ~ 
} 

public PieMenu(View view, Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // ~ 
} 

編輯1

public PieMenu(View view, Context context, AttributeSet attrs, int style) { 
    super(context, attrs, style); 
    // ~ 
} 
+0

我仍然繼續得到相同的錯誤。 – Hick

+0

奇怪,因爲'super(context,attrs);'應該照顧它。嘗試包括編輯。 – Vikram

+0

@vikram,您的代碼中存在打字錯誤,View,view => View view –

相關問題