尝试更新textview时出现空指针执行错误

时间:2015-09-03 08:20:31

标签: java pointers null

    package com.thenewboston.overflowmenu;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.graphics.Color;
    import android.os.Handler;
    import android.preference.PreferenceManager;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    import java.util.Calendar;


    public class MainActivity extends ActionBarActivity {

    TextView timerTextView;
    long startTime = 0;
     Handler timerHandler = new Handler();
     public Runnable timerRunnable = new Runnable() {

    @Override
    public void run() {

            Calendar current = Calendar.getInstance();
            Calendar usertime = Calendar.getInstance();
            usertime.set(Calendar.HOUR_OF_DAY, 20);
            usertime.set(Calendar.MINUTE, 00);
            usertime.set(Calendar.SECOND, 00);

            long millis = current.getTimeInMillis();

            timerTextview.settext(String.format("%02d:%02d:%02d",
            TimeUnit.MILLISECONDS.toHours(millis),
            TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
            TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
            timerTextView.setText(String.valueOf(millis));
     }

 };

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


    Calendar calendar = Calendar.getInstance();

    SharedPreferences pref =                     PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = pref.edit();//every change to the preferences must pass through an SharedPreferences.Editor, so call pref.edit() to retrive one;

    int count = 0;
    int mod;//to keep track of the current day
    boolean one = calendar.get(Calendar.HOUR) == 1  && calendar.get(Calendar.AM_PM) == Calendar.PM;
    if (calendar.get(Calendar.HOUR) == 3  && calendar.get(Calendar.AM_PM) == Calendar.PM) // if it is at 12 AM in the morning
    {

        count = pref.getInt("count", 1);
        mod = Math.abs(count % 5);


        if (mod == 0)
            tdy.setText("Biceps ");
        else if (mod == 1)
            tdy.setText("shoulders ");
        else if (mod == 2)
            tdy.setText("triceps ");
        else if (mod == 3)
            tdy.setText("lats ");
        else if (mod == 4)
            tdy.setText("chest ");


        if (this.isFinishing())//check if the user is leaving the app by pressing the back key, if true, we need to store the current count value which is the current day
        {
            editor.putInt("count", count);
            editor.apply(); //need to commit the change to persist it
        }

    }

}
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                while (!isInterrupted()) {
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                        timerHandler.postDelayed(timerRunnable, 0);
                        }
                    });

                }
            } catch (InterruptedException e) {
            }
        }
    };



@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) {
    RelativeLayout bg = (RelativeLayout) findViewById(R.id.bg);
    switch (item.getItemId()) {
        case R.id.Red: {
            if (item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
                bg.setBackgroundColor(Color.RED);
                Toast.makeText(MainActivity.this, "changing color to red", Toast.LENGTH_SHORT).show();
                return true;
            }
        }
        case R.id.green: {
            if (item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
                bg.setBackgroundColor(Color.GREEN);
                Toast.makeText(MainActivity.this, "changing color to green", Toast.LENGTH_SHORT).show();
                return true;
            }
        }
        case R.id.yellow: {
            if (item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
                bg.setBackgroundColor(Color.YELLOW);
                Toast.makeText(MainActivity.this, "Changing color to yellow", Toast.LENGTH_SHORT).show();
                return true;
            }
        }
        case R.id.main: {
            if (item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
                Intent intent = new Intent(this, MainActivity.class);
                bg.setBackgroundColor(Color.BLUE);
                startActivity(intent);
                Toast.makeText(MainActivity.this, "Main Menu", Toast.LENGTH_SHORT).show();
                return true;
            }
        }

        case R.id.setting: {
            if (item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
                Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
                Toast.makeText(MainActivity.this, "opening settings", Toast.LENGTH_SHORT).show();
                return true;
            }

        }
        case R.id.night: {
            if (item.isChecked()) {
                item.setChecked(false);


            } else {
                item.setChecked(true);
                bg.setBackgroundColor(Color.BLACK);
                this.setTheme(R.style.night);
                return true;
            }

        }
        case R.id.music: {
            if (item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
                bg.setBackgroundColor(Color.WHITE);
                Toast.makeText(MainActivity.this, "Opening..", Toast.LENGTH_SHORT).show();
                Intent one= getPackageManager().getLaunchIntentForPackage("com.android.music");
                startActivity(one);`
                return true;
            }
        }
        default:
            return super.onOptionsItemSelected(item);
    }
}
}

1 个答案:

答案 0 :(得分:0)

尝试进行以下更改:

 public class MainActivity extends ActionBarActivity {

    TextView timerTextView;

要:

 public class MainActivity extends ActionBarActivity {

    TextView timerTextView = new TextView();

    TextView timerTextView;
    ...

    @Override
    public void run() {
            timerTextView = (TextView) findViewById(R.id.timerTextView);
            ...
    }