Как я могу просмотреть мой ключ SSH для github?

Вы пытаетесь findViewById до того, как контекст активности будет готов.

Если вы хотите также получить доступ к timerf в других методах, создайте элемент класса, но выполните инициализацию в onCreate после setContentView:

TextView timerf;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timerf = (TextView) findViewById(R.id.timer);
}

Update:

Я не заметил других переменных-членов класса, которые вы создаете. Они ссылаются на timerf, где null.

Вы также должны инициализировать их в onCreate. Вот полный код, который должен работать:

package com.austinheitmann.stopwatch;

import android.os.CountDownTimer;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends ActionBarActivity {

    Timer timers = new Timer();
    TextView timerf;
    TimerTask time;
    int i=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timerf = (TextView) findViewById(R.id.timer);

        time = new TimerTask() {
            public void run() {
                i++;
                int k = i/60;
                int j = i%60;
                if (j>9) {
                    timerf.setText("seconds remaining: " + k + ":" + j);
                }else {
                    timerf.setText("seconds remaining: " + k + ":0" + j);
                }
                Log.d("Uhh", "Sec:" + i);
            }
        };
    }

    public void startB(View view) {
        timers.schedule(time, 100000000, 1000);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
13
задан Shawn 19 March 2012 в 18:56
поделиться