简体   繁体   中英

Android onClickListener works only once, Button does not works for second time

I am new in android Programming , Here is the code in which checkButton works only for once . Nothing happens when I click it for the second time. I can't seem to find problem.

 import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ProgressDialog progress; Button checkButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkButton = (Button) findViewById(R.id.button); checkButton.setOnClickListener( new View.OnClickListener(){ @Override public void onClick(View v){ EditText aNumber = (EditText) findViewById(R.id.editText); String aCount = aNumber.getText().toString(); boolean totalDigit = aCount.length()==16; if(totalDigit){ if(checkConnectivity()) { progress = new ProgressDialog(v.getContext()); progress.setTitle("Validating Number"); progress.setMessage("Processing..."); progress.setCancelable(true); progress.setProgressStyle(ProgressDialog.STYLE_SPINNER); progress.show(); new Thread(new Runnable() { public void run() { try { Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); } progress.dismiss(); MainActivity.this.runOnUiThread(new Runnable() { public void run() { //Do your UI operations like dialog opening or Toast here showAlert(); } }); } }).start(); }else{ Toast.makeText(MainActivity.this,"Internet Connection is not Available",Toast.LENGTH_SHORT).show(); }}else { Toast.makeText(MainActivity.this, "Please enter correct 16 digit number",Toast.LENGTH_SHORT).show(); } } }); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private void showAlert(){ AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("Number not found!"); builder.setCancelable(true); //Creating dialog box AlertDialog alert = builder.create(); //Setting the title manually alert.setTitle("Number Status"); alert.show(); setContentView(R.layout.activity_main); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private boolean checkConnectivity(){ ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) { return true; } else { return false; } } } 

My Check Button works for only once. After I press it for second time nothings happen. I have looked all previous answer related to this in stackoverflow but nothings helped me. Please someone tell me what is going wrong.

I think the problem is you call setContentView(R.layout.activity_main); twice. setContentView will override the layout and replace it with the new one. You should only one call it in onCreate . That's why you should remove this line in showAlert() method.

private void showAlert(){
         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

         builder.setMessage("Number not found!");
         builder.setCancelable(true);


         //Creating dialog box
         AlertDialog alert = builder.create();
         //Setting the title manually
         alert.setTitle("Number Status");
         alert.show();

         // remove this line below
         //setContentView(R.layout.activity_main);

     }

You will get the message warning Attempted to finish an input event but the input event receiver has already been disposed if you debug your code. The reason is you call setContentView twice so that the input event receiver of previous view is not free. That's why you can not click your button in the second times.

Try my solution, remove setContentView in showAlert and see the result.

You have two conditionals in your onClickListener which affect whether the onClicklistener "works"

Have you put in debug statements before these conditionals to see if they get this far (notice the two if statements below):

                boolean totalDigit = aCount.length()==16;
                if(totalDigit){
                 if(checkConnectivity()) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM