2012-04-11 2 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 이외의 것을 대상으로 사용합니다. 큰소리 야.

+0

도움에 대해 감사드립니다. – noloman

관련 문제