无法启动活动ComponentInfo NullPointExeption

时间:2014-11-17 05:44:02

标签: java android xml

我一直在学习如何编程和制作应用程序至今已有三个月了,而且我一直在尝试修复此错误的几天,我无法找到,代码的哪一部分有nullpoint :(   我非常感谢你的帮助,谢谢

错误日志:

11-16 23:27:27.618: E/AndroidRuntime(4298): FATAL EXCEPTION: main
11-16 23:27:27.618: E/AndroidRuntime(4298): Process: edu.uco.clam.onyourbike_chapter7, PID: 4298
11-16 23:27:27.618: E/AndroidRuntime(4298): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.uco.clam.onyourbike_chapter7/edu.uco.clam.onyourbike_chapter7.activities.TimerActivity}: java.lang.NullPointerException
11-16 23:27:27.618: E/AndroidRuntime(4298):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at android.os.Looper.loop(Looper.java:136)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at android.app.ActivityThread.main(ActivityThread.java:5001)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at java.lang.reflect.Method.invoke(Method.java:515)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at dalvik.system.NativeStart.main(Native Method)
11-16 23:27:27.618: E/AndroidRuntime(4298): Caused by: java.lang.NullPointerException
11-16 23:27:27.618: E/AndroidRuntime(4298):     at edu.uco.clam.onyourbike_chapter7.activities.BaseActivity.setupActionBar(BaseActivity.java:57)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at edu.uco.clam.onyourbike_chapter7.activities.BaseActivity.onCreate(BaseActivity.java:85)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at edu.uco.clam.onyourbike_chapter7.activities.TimerActivity.onCreate(TimerActivity.java:65)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at android.app.Activity.performCreate(Activity.java:5231)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-16 23:27:27.618: E/AndroidRuntime(4298):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
11-16 23:27:27.618: E/AndroidRuntime(4298):     ... 11 more

这里是BaseActivity文件:

public class BaseActivity extends Activity {

private static String CLASS_NAME;

private NightMode nightMode;
private Notify notify;

public boolean canGoHome = true;

public BaseActivity() {
    CLASS_NAME = getClass().getName();
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(canGoHome);
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Make sure we do nothing silly!
    if (BuildConfig.DEBUG) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectAll().penaltyLog().build());

        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectAll().penaltyLog().penaltyDeath().build());
    }

    int themeID = getIntent().getIntExtra("Theme", -1);
    if (themeID != -1) {
        setTheme(themeID);
    }
    boolean isLightTheme = getIntent()
            .getBooleanExtra("isLightTheme", true);

    notify = new Notify(this);

    nightMode = new NightMode(getWindow(), this, true, isLightTheme);

    setupActionBar();
}

@Override
public void onResume() {
    super.onResume();

    // nightMode.startSensing();
}

@Override
public void onPause() {
    super.onPause();

    // nightMode.stopSensing();
}

private void gotoRoutes() {
    Log.d(CLASS_NAME, "gotoRoutes");

    Intent routes = new Intent(this, RoutesActivity.class);

    startActivity(routes);
}

/**
 * Called when the settings button is clicked on.
 * 
 * @param view
 * the button clicked on
 */
public void gotoSettings(View view) {
    Log.d(CLASS_NAME, "gotoSettings");

    Intent settings = new Intent(this, SettingsActivity.class);

    startActivity(settings);
}

private void gotoHome() {
    Log.d(CLASS_NAME, "gotoHome");

    Intent timer = new Intent(this, TimerActivity.class);
    timer.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    startActivity(timer);
}

public void gotoMap() {
    Log.d(CLASS_NAME, "gotoMap");

    Intent map = new Intent(this, MapActivity.class);

    startActivity(map);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        gotoHome();
        return true;
    case R.id.menu_settings:
        gotoSettings(null);
        return true;
    case R.id.menu_routes:
        gotoRoutes();
        return true;
    case R.id.menu_map:
        gotoMap();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

public void goBack(View view) {
    finish();
}

public void notification(String title, String message) {
    notify.notify(title, message);
}

}

这里是TimerActivity文件:

public class TimerActivity extends BaseActivity {

private static String CLASS_NAME;

private static long UPDATE_EVERY = 200;

private TextView counter;
private Button start;
private Button stop;
private Handler handler;
private Vibrator vibrate;
private UpdateTimer updateTimer;
private final TimerState timer;
private long lastSeconds;

public TimerActivity() {
    CLASS_NAME = getClass().getName();
    timer = new TimerState();
    canGoHome = false;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d(CLASS_NAME, "onCreate");

    setContentView(R.layout.activity_timer);

    // NOTE findViewById must be called after setContentView or we'll get an
    // RTE
    counter = (TextView) findViewById(R.id.timer);
    start = (Button) findViewById(R.id.start_button);
    stop = (Button) findViewById(R.id.stop_button);

    // NOTE some phone and tablets may not vibrate
    vibrate = (Vibrator) getSystemService(VIBRATOR_SERVICE);

    if (vibrate == null) {
        Log.w(CLASS_NAME, "No vibration service exists.");
    }

    timer.reset();

    stayAwakeOrNot();
}

// Note must be public or RTE

/**
 * Called when the start button is clicked on.
 * 
 * @param view
 * the button clicked on
 */
public void clickedStart(View view) {
    Log.d(CLASS_NAME, "clickedStart");

    timer.start();

    enableButtons();

    handler = new Handler();
    updateTimer = new UpdateTimer(this);
    handler.postDelayed(updateTimer, UPDATE_EVERY);

    lastSeconds = -1; // for initial notification
}

/**
 * Called when the stop button is clicked on.
 * 
 * @param view
 * the button click on
 */
public void clickedStop(View view) {
    Log.d(CLASS_NAME, "clickedStop");

    timer.stop();

    enableButtons();

    handler.removeCallbacks(updateTimer);
    updateTimer = null;
    handler = null;
}

@Override
public void onStart() {
    super.onStart();
    Log.d(CLASS_NAME, "onStart");

    if (timer.isRunning()) {
        handler = new Handler();
        updateTimer = new UpdateTimer(this);
        handler.postDelayed(updateTimer, UPDATE_EVERY);
    }
}

/**
 * Enable/disable the stop and start buttons
 */
protected void enableButtons() {
    Log.d(CLASS_NAME, "enableButtons");

    start.setEnabled(!timer.isRunning());
    stop.setEnabled(timer.isRunning());
}

/**
 * Change the counter text view to display the current formatted time
 */
protected void setTimeDisplay() {
    Log.d(CLASS_NAME, "setTimeDisplay");

    counter.setText(timer.display());
}

@Override
public void onResume() {
    super.onResume();
    Log.d(CLASS_NAME, "onResume");
    enableButtons();
    setTimeDisplay();
}

@Override
public void onPause() {
    super.onPause();
    Log.d(CLASS_NAME, "onPause");
}

@Override
public void onStop() {
    super.onStop();
    Log.d(CLASS_NAME, "onStop");

    Settings settings = ((OnYourBike) getApplication()).getSettings();

    if (timer.isRunning()) {
        handler.removeCallbacks(updateTimer);
        updateTimer = null;
        handler = null;
    }

    if (settings.isCaffeinated(this)) {
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(
                PowerManager.FULL_WAKE_LOCK, CLASS_NAME);

        if (wakeLock.isHeld()) {
            wakeLock.release();
        }
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(CLASS_NAME, "onDestroy");
}

@Override
public void onRestart() {
    super.onRestart();
    Log.d(CLASS_NAME, "onRestart");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.d(CLASS_NAME, "onCreateOptionsMenu");

    getMenuInflater().inflate(R.menu.activity_main, menu);

    return true;
}

/**
 * Keep the screen on depending on the stay awake setting
 */
protected void stayAwakeOrNot() {
    Log.d(CLASS_NAME, "stayAwakeOrNot");

    Settings settings = ((OnYourBike) getApplication()).getSettings();

    if (settings.isCaffeinated(this)) {
        // Log.i(CLASS_NAME, "Staying awake");
        getWindow()
                .addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    } else {
        // Log.i(CLASS_NAME, "Not staying awake");
        getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
}

// NOTE could be an anonymous inner class - easier to understand this way
class UpdateTimer implements Runnable {

    Activity activity;

    public UpdateTimer(Activity activity) {
        this.activity = activity;
    }

    /**
     * Updates the counter display and vibrated if needed
     */
    public void run() {
        // Log.d(CLASS_NAME, display);
        Settings settings = ((OnYourBike) getApplication()).getSettings();

        setTimeDisplay();

        if (timer.isRunning()) {
            if (settings.isVibrateOn(activity)) {
                vibrateCheck();
            }
            notifyCheck();
        }

        stayAwakeOrNot();

        if (handler != null) {
            handler.postDelayed(this, UPDATE_EVERY);
        }
    }

    protected void vibrateCheck() {
        long seconds;
        long minutes;

        Log.d(CLASS_NAME, "vibrateCheck");

        timer.elapsedTime();
        seconds = timer.seconds();
        minutes = timer.minutes();

        // NOTE done this way to avoid Array/ArrayList issues
        // NOTE hasVibrator() only on API 11+
        // NOTE try/catch to stop force close on emulator
        // NOTE very easy to get manifest wrong!
        try {
            // NOTE seconds != lastSeconds so it only vibrates once/second
            if (vibrate != null && seconds == 0 && seconds != lastSeconds) {
                long[] once = { 0, 100 };
                long[] twice = { 0, 100, 400, 100 };
                long[] thrice = { 0, 100, 400, 100, 400, 100 };

                // every hour
                if (minutes == 0) {
                    Log.i(CLASS_NAME, "Vibrate 3 times");
                    vibrate.vibrate(thrice, -1);
                }
                // every 15 minutes
                else if (minutes % 15 == 0) {
                    Log.i(CLASS_NAME, "Vibrate 2 time");
                    vibrate.vibrate(twice, -1);
                }
                // every 5 minutes
                else if (minutes % 5 == 0) {
                    Log.i(CLASS_NAME, "Vibrate once");
                    vibrate.vibrate(once, -1);
                }
            }
        } catch (Exception e) {
            Log.w(CLASS_NAME, "Exception: " + e.getMessage());
        }

        lastSeconds = seconds;
    }

    protected void notifyCheck() {
        long seconds;
        long minutes;
        long hours;

        Log.d(CLASS_NAME, "notifyCheck");

        timer.elapsedTime();
        seconds = timer.seconds();
        minutes = timer.minutes();
        hours = timer.hours();

        if (minutes % 15 == 0 && seconds == 0 && seconds != lastSeconds) {
            String title = getResources().getString(R.string.time_title);
            String message = getResources().getString(
                    R.string.time_running_message);

            if (hours == 0 && minutes == 0) {
                message = getResources().getString(
                        R.string.time_start_message);
            }

            message = String.format(message, hours, minutes);

            notification(title, message);
        }

        lastSeconds = seconds;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

CLASS_NAME似乎是空的。

将log标记字符串设为final并在声明点初始化它:

private static final String CLASS_NAME = TimerActivity.class.getName();

P.S。为什么要覆盖构造函数?由于TimerState对象是最终的并且不需要任何参数,您可以在声明点创建它,初始工作可以在onCreate方法而不是在构造函数中完成