简体   繁体   中英

How to debug an Android app force close error?

public class MainActivity extends Activity {

EditText oETextN;
EditText oETextPh;
Button oButtonSave;
public String givenName;
public String givenPhNum;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    findViewsById();
    oButtonSave.setOnClickListener(new OnClickListener(){
        public void onClick(View view){
            givenName = oETextN.getText().toString();
            givenPhNum = oETextPh.getText().toString();
            OrgiOperations.Insert2Contacts(getApplicationContext(),
                    givenName, givenPhNum);
            if (OrgiOperations.isTheNumberExistsinContacts(
                    getApplicationContext(), givenPhNum)) {
                Log.i(OrgiOperations.TAG, "Exists");
            } else {
                Log.i(OrgiOperations.TAG, "Not Exists");
            }

        }

    });

    setContentView(R.layout.activity_main);

}

private  void findViewsById(){
    oButtonSave = (Button) findViewById(R.id.SaveBtn);
    oETextN = (EditText) findViewById(R.id.FLName);
    oETextPh = (EditText) findViewById(R.id.Ph);

    //oEText = (EditText) findViewById(R.id.Name);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

I've been trying to make a simple Android app that saves the entered details (Name and Phone Number) directly to your device phonebook. This is the MAIN activity, the other class includes the functions to do this, which I'm sure are implemented correctly.

The problem rises when I run the application on my phone, and unluckily after hours of cold coding I get the FORCE CLOSE error!

Change to

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main); 
 initializeViews();

Then

  private  void initializeViews(){
    oButtonSave = (Button) findViewById(R.id.SaveBtn);
    oETextN = (EditText) findViewById(R.id.FLName);
    oETextPh = (EditText) findViewById(R.id.Ph);
    //oEText = (EditText) findViewById(R.id.Name); 
  }

You need to set the content of the layout first to the activity then initialize views.

Also rename findViewsById(); to initializeViews() or something more appropriate coz you initialize your views with findViewById as oButtonSave = (Button) findViewById(R.id.SaveBtn);

Edit:

Have the click listener after initialization of view.

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main); 
 oButtonSave = (Button) findViewById(R.id.SaveBtn);
 oETextN = (EditText) findViewById(R.id.FLName);
 oETextPh = (EditText) findViewById(R.id.Ph);
    oButtonSave.setOnClickListener(new OnClickListener(){
    public void onClick(View view){
        givenName = oETextN.getText().toString();
        givenPhNum = oETextPh.getText().toString();
        OrgiOperations.Insert2Contacts(getApplicationContext(),
                givenName, givenPhNum);
        if (OrgiOperations.isTheNumberExistsinContacts(
                getApplicationContext(), givenPhNum)) {
            Log.i(OrgiOperations.TAG, "Exists");
        } else {
            Log.i(OrgiOperations.TAG, "Not Exists");
        }

    }

});
}

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