2013-10-11 3 views
0

어떻게 든 나는 내 onClickListener를 도살 한 것 같다. 어떤 이유로 든 갑자기 goButton을 클릭하면 아무 것도하지 않습니다. 나는 코드를 꽤 자주 살펴 보았고 내가 잘못한 것을 여기에서 보지 못했다. 어떤 제안?OnClickListener는 더 이상 작동하지 않습니다.

//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(); 
         } 
        } 
       } 

      } 

     } 

답변

1

범인은 이리

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

당신이 onCreate 메서드 내 로컬 goButton를 선언하고, 그 변수에 리스너를 설정하는 수 있습니다. 나는 onClick 실제로 발사되고 있음을 추측하고있어,하지만이

if (v == goButton) 

실패 할 수있다 질환이. 첫 번째 줄을 업데이트하십시오 .-

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

아래에있는 동일한 줄을 제거하십시오. 또한, onClick이 실제로 호출되지 않았는지 또는 문제가 실제로 if 조건에 있는지 확인하기 위해 단계별로 디버깅하는 것이 유용 할 것입니다.

+0

줄을 제거하면 강제 종료 오류가 발생합니다. – HelloMojo

+0

내 잘못입니다. 아래에서'goButton = (Button) findViewById (R.id.go_button);'을하고있는 것을 보았습니다 만, 전에 액세스하려고했습니다. 선을 제거하는 대신'Button' 부분 만 제거하십시오. 아래 두 번째'goButton = (Button) findViewById (R.id.go_button);은 필요 없습니다. – ssantos

관련 문제