0

I have commented out the ((Button) findViewById(R.id.addClockIn)).setOnClickListener(this) section and it doesn't crash but the buttons will not work.

public class MainActivity extends Activity implements OnClickListener {
TextView textView;

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
        ((Button) findViewById(R.id.addClockIn)).setOnClickListener(this);
    }
}

    public void addClockIn(View view) {
        StringBuilder str = new StringBuilder("It worked");
        textView.setText(str);
    }
  • 1
    When you get a crash, go to Logcat and find the stack trace for the crash. If you don't understand what causes the crash, post the stack trace here. – Karakuri May 23 '14 at 14:41
  • Also, `((Button) findViewById(R.id.addClockIn)).setOnClickListener(this);` should be **outside** the `if (savedInstanceState == null)` block. – Karakuri May 23 '14 at 14:42
  • Likely the button is not in `activity_main` layout but in the fragment layout. For help with that, see http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – laalto May 23 '14 at 15:00

2 Answers2

0

I believe the issue was that you were not calling your on click listener properly also, the onclick listener needed to be moved outside of the savedinstancestate==null block. see code as follows.

public class MainActivity extends Activity {
    TextView textView;

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

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();

        }
        ((Button) findViewById(R.id.addClockIn)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder str = new StringBuilder("It worked");
                textView.setText(str);
            }
        });
    }
}

Also, is the text view hooked up anywhere so that you can see what it is saying? I dont see you attaching it to a UI element at any point.

Sean C.
  • 228
  • 2
  • 8
-1

You shouldn't be passing in this into your setOnClickListener, but rather a type of OnClickListener itself. I would remove implements OnClickListener in your class declaration and just change your code to be:

((Button) findViewById(R.id.addClockIn)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             // Put whatever action you want to happen in here
             // Such as:
             StringBuilder str = new StringBuilder("It worked");
             textView.setText(str);
        }
    });

This way, you can assign click listeners to multiple views, each implementing their own action when they are click. Be careful though, currently this will probably give an error, as you have only declared your TextView and not defined it with the findViewById() method.

krodmannix
  • 845
  • 10
  • 30