1

I am learning Android, and this is the first time I encounter unsolvable error (for me), so I decided to ask you guys. Sorry for the messy post.

I have been trying to assign the Button to receive clicks, but then every time I put in a setOnClickListener(this) in my MainActivity.java, it crash.

public class MainActivity extends ActionBarActivity implements View.OnClickListener{

Button add_fragment_button;

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



    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new Overview())
                .commit();
    }

    add_fragment_button = (Button) findViewById(R.id.add_wallet_button); 
    //this line still work perfectly
    add_fragment_button.setOnClickListener(this); 
    //crash here
}

This crash, I assume, cause by the setOnClickListener(), because I have made sure that the R.id is correct.

Please help. And also, sorry for my English.

Edit: Ok, thanks to the helpful comments I found out that the button is on the 'Overview.xml'. I've fixed that but then it came to another problem. The fragment manager replace the Overview fragment with another one, but they both appear on the screen, overlaying each other.

DeanK
  • 344
  • 2
  • 14

1 Answers1

0

i think your add_fragment_button will be null because your button should be inside of OverView class. please do one thing in overview class override method oncreateview and inflate new layout in oncreateview method and then add button add_fragment_button in this layout and then write add_fragment_button.setOnClickListener(this); inside overview fragment.I think this will be work.thanks;-)

bhavesh kaila
  • 761
  • 1
  • 5
  • 25