Viewpager +按钮添加到新活动

时间:2016-04-25 19:27:31

标签: java

所以,我在一个Activity中用Viewpager编写了代码。我把按钮放在新活动的按钮上,但它不起作用,我不知道为什么。

我尝试使用标准的Intent函数,没有警告或错误,但是当我使用按钮时应用程序崩溃。我不知道问题出在哪里? 第二个Activity是标准的,new Activity和button有一些id和方法onClick。

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class customAdapter extends PagerAdapter {

    public int[] levels = {R.drawable.ip_sem_jeden, R.drawable.ip_sem_dwa, R.drawable.ip_sem_trzy, R.drawable.ic_sem_czt,
            R.drawable.ip_sem_piat, R.drawable.ip_sem_szes};

    private Context ctx;
    private LayoutInflater layoutInflater;

    public customAdapter(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    public int getCount() {
        return levels.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View item_view = layoutInflater.inflate(R.layout.swipe_select_stage, container, false);
        ImageView imageView = (ImageView) item_view.findViewById(R.id.image_view);
        imageView.setImageResource(levels[position]);
        container.addView(item_view);
        Button button = (Button) item_view.findViewById(R.id.startGameButton);
        button.setText(ctx.getString(R.string.nameSemestr) + ++position);
        return item_view;


    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }
}

0 个答案:

没有答案