2

ユーザーが 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

1 に答える 1

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 を割り当てたほうがよい場合があります。そのため、MeerActivity 以外のものをターゲットとして使用する場合に備えて、TopBarView をもう少し再利用できます。大したことはありません。

于 2012-04-11T14:34:05.890 に答える