-1

I'm trying to build a simple app to run a Chronometer. Once I edited my Java code, the app just force closes when I try running it.

package com.example.debatetimer;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;

public class MainActivity extends Activity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button startbutt = (Button) findViewById(R.id.startbutton);
        startbutt.setOnClickListener(this);
        Button stopbutt = (Button) findViewById(R.id.stopbutton);
        stopbutt.setOnClickListener(this);
        Button resetbutt = (Button) findViewById(R.id.resetbutton);
        resetbutt.setOnClickListener(this);
    }
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
        case R.id.startbutton:{
            startChronometer(v);
        }
        case R.id.stopbutton:{
            stopChronometer(v);
        }
        case R.id.resetbutton:{
            resetChronometer(v);
        }
        }
    }

    public void startChronometer(View view)
    {
        ((Chronometer)findViewById(R.id.chronometer1)).start();
    }

    public void stopChronometer(View view)
    {
        ((Chronometer) findViewById(R.id.chronometer1)).stop();
    }

    public void resetChronometer(View view)
    {
        ((Chronometer) findViewById(R.id.chronometer1)).setBase(SystemClock.elapsedRealtime());
    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

I've just started learning Android on my own, and it's a huge jump from simple Java. Thanks for all the help!

2 Answers2

5

I think your forget to set layout in your Activity like

setContentView(R.layout.yourlayout);

In onCreate(....) method and before your View reference

Ryan M
  • 18,333
  • 31
  • 67
  • 74
M D
  • 47,665
  • 9
  • 93
  • 114
2

Try this..

You missed setContentView(R.layout.layoutname);

add that before initialization of Button like below

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layoutname);
    Button startbutt = (Button) findViewById(R.id.startbutton);
Hariharan
  • 24,741
  • 6
  • 50
  • 54