0

不知何故,我似乎已经屠杀了我的 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();
                        }
                    }
                }

            }

        }
4

1 回答 1

1

罪魁祸首可能在这里-

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

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

if (v == goButton)

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

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

并删除您在下面的相同行。此外,逐步调试,检查是否onClick没有真正调用,或者问题if确实存在,这将很有用。

于 2013-10-11T18:08:50.350 回答