开发者

On Click method not working for buttons gotten by inflating the layout!

Please don't answer alr开发者_JAVA百科eady answered

I already asked this question, but I think I wasn't clear enough! My onClick methods for buttons that were obtained by inflating a layout are not working how do I fix this have I forgot to do something in my code?

Updated: Now with the following code I get a nullpointerexception when I set the onClickListener what is wrong

Here is some of my code: (note it is not complete you won't see onCreate!)

         LayoutInflater  inflater = LayoutInflater.from(getBaseContext());
    View playv = inflater.inflate(R.layout.play, null);
    Button updateLevel = (Button) playv.findViewById(R.id.updateLevel);

    updateLevel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setLevelOnClick(v);
            }
        });


    View levelv = inflater.inflate(R.layout.level, null);
    Button gotomenu = (Button) levelv.findViewById(R.id.tomenu);

    gotomenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toMenuOnClick(v);
            }
        });


Andrew,

Try:

LayoutInflater  inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.root_view, null);
Button updateLevel = (Button) v.findViewById(R.id.updateLevel);

updateLevel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setLevelOnClick(v);
        }
    });
Button goBackMainMenu = v.inflate(R.layout.play, null).findViewById(R.id.tomenu);
goBackMainMenu.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        toMenuOnClick(v);
    }
});

this is assuming root_view is the View containing the Button objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜