2012-04-11 75 views
2

我需要在用戶單擊一個在FrameLayout中呈現的按鈕後開始一個新活動。它呈現我想要用戶點擊的按鈕,但當然它現在沒有做任何事情。在FrameLayout中開始活動

該類的代碼如下,但我不能調用startActivity(intent)。

public class TopBarView extends FrameLayout { 

    private ImageView mLogoImage; 
    private Button mInfoButton; 

    public TopBarView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public TopBarView(Context context) { 
     super(context); 
     init(); 
    } 

    public TopBarView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    private void init() { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.top_bar, null); 

     mLogoImage = (ImageView) view.findViewById(R.id.imageLogo); 
     mInfoButton = (Button) view.findViewById(R.id.infoButton); 

     mInfoButton.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // We load & render the view for the information screen 
//    Intent i = new Intent(); 
//    i.setClass(getContext(), MeerActivity.class); 
//    startActivity(i); 
      } 
     }); 

     addView(view); 
    } 
} 

非常感謝!

回答

4

變化:

public void onClick(View v) { 
// We load & render the view for the information screen 
//    Intent i = new Intent(); 
//    i.setClass(getContext(), MeerActivity.class); 
//    startActivity(i); 
} 

要:

public void onClick(View v) { 
// We load & render the view for the information screen 
    Intent i = new Intent(); 
    i.setClass(v.getContext(), MeerActivity.class); 
    v.getContext().startActivity(i); 
} 

注:可能是更好的分配通過使用的是這樣的TopBarView活動的onclicklistener更可重複使用的情況下,有些你想使用除MeerActivity以外的其他內容作爲目標。沒有biggy tho。

+0

非常感謝您的幫助! – noloman 2012-04-11 14:52:41