-1

I have the following code and whenever I start to run it, it will display the message Unfortunately has stopped. It used to run before but now it is not. I have no errors in my program. Any idea what the problem might is pls? Below is my code:

Code in Main Activity:when button is clicked the message shows up unfortunately etc..

public class ContactsMain extends AppCompatActivity {

public static final int REQUEST_CODE_AddContact=1;

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

    Button b = (Button) findViewById(R.id.addContact);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View a) {

            Intent i = new Intent(a.getContext(), AddContact.class);

            startActivityForResult(i, REQUEST_CODE_AddContact);
        }
    });
}

}

Activity AddContact code:

package com.example.maria.maria_caruana_1bsc5;


public class AddContact extends AppCompatActivity {

    public static final int REQUEST_CODE_ContactsMain=3;

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

        Button b1 = (Button) findViewById(R.id.btnGoBack);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View d) {

                Intent i = new Intent(d.getContext(), ContactsMain.class);

                startActivityForResult(i, REQUEST_CODE_ContactsMain);
            }
        });

    }
}
JAAD
  • 12,349
  • 7
  • 36
  • 57
  • 1
    You can use `getApplicationContext()`in `Intent`. Simply try this `Intent i = new Intent(getApplicationContext(), ContactsMain.class);`. – Jay Rathod Apr 20 '16 at 11:17

1 Answers1

1

Change your intent in onClick override method like:

Intent i = new Intent(ContactsMain.this, AddContact.class);

similar in other class too..

Jhaman Das
  • 1,094
  • 13
  • 28