我似乎以某种方式屠杀了我的onClickListener。出于某种原因,当我单击goButton时,它什么都不做。我已经看了很多遍代码了,似乎无法发现我在这里做错了什么。有什么建议么?

//This activity displays the start layout of the app
    public class StartActivity extends Activity implements OnClickListener {
        private AnimationDrawable mGoButtonAnimation;
        Button goButton;
        Context c;
        boolean isAirPlaneMode, isMDNPresent = false;// boolean values to check for
        // airplane mode and if the
        // sim populates the MDN
        int simState;
        TelephonyManager tm;
        boolean NetworkConnection = false;// boolean to check the Network
        // Availability
        AlertDialog mConfirmAlert = null;
        TextView text;
        TextView mUpdatetext;
        int version;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.start);
            Button goButton = (Button) findViewById(R.id.go_button);
            // Set GO button to drawable animation
            goButton.setBackgroundResource(R.drawable.go_button_animation);
            mGoButtonAnimation = (AnimationDrawable) goButton.getBackground();

            version = android.os.Build.VERSION.SDK_INT;
            tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            // to read the SIM state
            simState = tm.getSimState();
            System.out.println("Sim State" + simState);
            if (tm.getLine1Number() == null) {
                showAlert("Insert an active  SIM.");
            }
            // to check for MDN
            else if (tm.getLine1Number().equalsIgnoreCase("")) {
                isMDNPresent = true;
            }

            String texts = "";
            CharSequence styledText = texts;

            goButton = (Button) findViewById(R.id.go_button);

            text = (TextView) findViewById(R.id.text);

            // text for go button
            texts = String.format(getString(R.string.start_text));

            styledText = Html.fromHtml(texts);

            text.setText(styledText);

            // to check for the network availability
            NetworkConnection = CheckNetworkAvailability
                    .CheckforNetworkAvailability(StartActivity.this);
            if (!NetworkConnection) {
                showAlert("Network is not available, Check for the Network Connections");
            }
            // to check for airplane mode
            isAirPlaneMode = isAirplaneModeOn(StartActivity.this);
            goButton.setOnClickListener(this);

        }

        public void onClick(View v) {
            if (v == goButton) {
                //

                if (simState == TelephonyManager.SIM_STATE_ABSENT) {
                    showAlert("Sim Card is absent, Please insert a   Sim Card");

                } else if (simState == TelephonyManager.SIM_STATE_UNKNOWN) {
                    showAlert("Sim Card is absent, Please insert a   Sim Card");
                } else if (isAirPlaneMode != false) {

                    showAlert("Please Insert a  Sim Card or Turn on the AirPlane Mode and Re-Run the app");

                } else if (simState == TelephonyManager.SIM_STATE_NETWORK_LOCKED
                        || simState == TelephonyManager.SIM_STATE_PIN_REQUIRED
                        || simState == TelephonyManager.SIM_STATE_PUK_REQUIRED
                        || simState == TelephonyManager.SIM_STATE_UNKNOWN) {

                    showAlert("Sim Card is absent, Please insert a  Sim Card");

                } else if (simState == TelephonyManager.SIM_STATE_READY) {
                    if (version < VERSION_CODES.ICE_CREAM_SANDWICH) {// Pre-ICS
                        if (isMDNPresent) {
                            // start SaveMDN activity
                            Intent i = new Intent(StartActivity.this, SaveMDN.class);
                            startActivity(i);
                            finish();
                        } else {
                            // start ActivityForPreICS activity
                            Intent i = new Intent(StartActivity.this,
                                    ActivityForPreICS.class);
                            startActivity(i);
                            finish();
                        }
                    } else {
                        // ICS and UP
                        if (isMDNPresent) {
                            // start SaveMDN activity
                            Intent i = new Intent(StartActivity.this, SaveMDN.class);
                            startActivity(i);
                            finish();
                        } else {
                            // start Update Activity
                            Intent i = new Intent(StartActivity.this,
                                    UpdateActivity.class);
                            startActivity(i);
                            finish();
                        }
                    }
                }

            }

        }

最佳答案

罪魁祸首可能在这里。

Button goButton = (Button) findViewById(R.id.go_button);


您要在goButton方法内部本地声明onCreate,并将侦听器设置为该变量。我猜测onClick实际上是被解雇了,但是这种情况。

if (v == goButton)


可能会失败。更新第一行。-

goButton = (Button) findViewById(R.id.go_button);


并删除下面的相同行。此外,逐步调试,检查是否未真正调用onClick或问题确实在if条件下也很有用。

08-05 08:22