开发者

can someone help me with why my OnClickListener won't work? Android

Is there something simple i might be missing? The "kruis" picture shows up on my ImageButton, so i'm pretty sure my main.xml is good, but when i click on the ImageButton, i get no Toast and testView does not change... been struggling for a few hours on this now, not sure what i'm doing wrong!

package com.matchit;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class matchit extends Activity {

OnClickListener cardListener;
TextView testView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    开发者_StackOverflow中文版setContentView(R.layout.main);

    testView = (TextView)findViewById(R.id.test);

    ImageButton b1 = (ImageButton)findViewById(R.id.card1);

    b1.setImageResource(R.drawable.kruis);
    b1.setOnClickListener(cardListener);

    cardListener = new OnClickListener(){

        @Override
        public void onClick(View v) {

            testView.setText("its working");
            Toast.makeText(getApplicationContext(),
                    "its working",
                     Toast.LENGTH_LONG).show();
        }
    };
}

}


Value of cardListener is null at this line:

  b1.setOnClickListener(cardListener);

Just move this line after cardListener = new OnClickListener(){

cardListener = new OnClickListener(){

    @Override
    public void onClick(View v) {

        testView.setText("its working");
        Toast.makeText(getApplicationContext(),
                "its working",
                 Toast.LENGTH_LONG).show();
    }
};

// moved down
b1.setOnClickListener(cardListener);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜