2012-04-11 20 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); 
} 

注:使用している活動を経てonclicklistenerを割り当てるには良いかもしれないので、TopBarViewはあなたがしたい場合には、もう少し再利用可能ですMeerActivity以外のものをターゲットとして使用する。いいえ、大したことはありません。

+0

ありがとうございました! – noloman

関連する問題