Android developers --public class MainActivity extends AppCompatActivity implements View.OnClickListener not run

  Android started to learn, and recorded some of TD's bug and issue ......

  When the code in the follow-up "the Android first line of code," second edition, at page 82 of the code sample is probably about this:

 1 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 2 
 3     private EditText editText;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         editText = (EditText)findViewById(R.id.edit_text);
10         Button button = (Button)findViewById(R.id.bottom);
11         button.setOnClickListener(MainActivity.this);
12     }
13 
14     @Override
15     public void onClick(View v) {
16         switch (v.getId()){
17             case R.id.bottom:
18                 String inputText = editText.getText().toString();
19                 Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show();
20                 break;
21             default:
22                 break;
23         }
24     }
25 }

  This method is still in very good shape, the direct successor View.OnClickListener interfaces in our MainActivity, in a class replication onClick method, however! ! !

  After entering the simulator can not start the program, the display stop running!

  Internet search a bit reasons, always finding out the point, I did not find the root cause of problems.

  Well, experiments, step by step, knock, first on the new method implements View.OnClickListener delete error ...... Sure enough hands to do so.

  Get solutions:

  That will be deleted implements View.OnClickListener relevant code.

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     private EditText editText;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         editText = (EditText)findViewById(R.id.edit_text);
10         Button button = (Button)findViewById(R.id.bottom);
11     }
12 
13 }
View Code

  Then according to their needs, using a general way to set the appropriate button can be solved. Not going to repeat.

 

Guess you like

Origin www.cnblogs.com/swust-rjgc-1705-monitor/p/12574461.html