使用Log.e()时出错;

时间:2014-12-26 20:52:55

标签: java android logging logcat

我的应用程序一直崩溃,所以我尝试尝试并使用catch块来查找错误

try{
        sm = (SensorManager) this.activity.getSystemService(this.activity.SENSOR_SERVICE);
        acceleration = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this, acceleration, SensorManager.SENSOR_DELAY_NORMAL);
    }
    catch(Exception f){
        Log.e("Error initializing sensors in GameScene", f.getLocalizedMessage());
    }

然后我检查了logcat的错误,发现它给我的错误实际上是Log方法的错误

12-26 12:43:52.094: E/AndroidRuntime(17183): FATAL EXCEPTION: AsyncTask #1
12-26 12:43:52.094: E/AndroidRuntime(17183): Process: com.magnusworks.justtic_tac_toe, PID: 17183
12-26 12:43:52.094: E/AndroidRuntime(17183): java.lang.RuntimeException: An error occured while executing doInBackground()
12-26 12:43:52.094: E/AndroidRuntime(17183):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.lang.Thread.run(Thread.java:841)
12-26 12:43:52.094: E/AndroidRuntime(17183): Caused by: java.lang.NullPointerException: println needs a message
12-26 12:43:52.094: E/AndroidRuntime(17183):    at android.util.Log.println_native(Native Method)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at android.util.Log.e(Log.java:334)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at com.magnusworks.justtic_tac_toe.GameScene.create(GameScene.java:49)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at com.magnusworks.justtic_tac_toe.SceneManager$1.doInBackground(SceneManager.java:37)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at com.magnusworks.justtic_tac_toe.SceneManager$1.doInBackground(SceneManager.java:1)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-26 12:43:52.094: E/AndroidRuntime(17183):    ... 4 more

我做了一些研究而且找不到类似的东西....我是不正确地使用Log.e?

1 个答案:

答案 0 :(得分:1)

日志调用的第二个参数必须为非null。 getLocalizedMessage()可以返回null

考虑使用日志调用的三个arg版本来记录完整的异常堆栈跟踪,例如。

Log.e("GameScene", "Error initializing sensors in GameScene", f);

第一个参数是日志标记,按照惯例,它可以用来过滤日志。