Java 在 Android 中登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19136448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Log.e in Android
提问by Rgarg
I am trying to Log in my program so that it helps me debug but the Log statement itself is giving an error. I wrote the log message like this way
我正在尝试登录我的程序,以便它帮助我调试,但 Log 语句本身出现错误。我这样写日志消息
import android.util.Log;
public static final String TAG = "MyActivity";
Log.e(TAG,"I shouldn't be here");
This is the statement I have put up in the public class. It gives the error :
这是我在公开课上发表的声明。它给出了错误:
1. Syntax error on token "(", delete this token.
2. Syntax error on token, Variable Declarator Expected instead.
I am new to android SDK development and Java so any help will be appreciated.
我是 android SDK 开发和 Java 的新手,所以任何帮助将不胜感激。
Thanks.
谢谢。
I am putting in my Exact code here:
我在这里输入我的确切代码:
package com.android.record;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.util.Log;
//import com.android.record.R;
public class AudioRecordTest extends Activity {
private static final int RECORDER_SAMPLERATE = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private AudioRecord recorder = null;
private Thread recordingThread = null;
private boolean isRecording = false;
public static final String TAG = "MyActivity";
@Override
Log.e(TAG,"I shouldn't be here");
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_record_test);
setButtonHandlers();
enableButtons(false);
int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
}
private void setButtonHandlers() {
((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
}
private void enableButton(int id, boolean isEnable) {
((Button) findViewById(id)).setEnabled(isEnable);
}
private void enableButtons(boolean isRecording) {
enableButton(R.id.btnStart, !isRecording);
enableButton(R.id.btnStop, isRecording);
}
int BufferElements2Rec = 1024; // want to play 2048 (2K) since 2 bytes we use only 1024
int BytesPerElement = 2; // 2 bytes in 16bit format
private void startRecording() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
}
//Conversion of short to byte
private byte[] short2byte(short[] sData) {
int shortArrsize = sData.length;
byte[] bytes = new byte[shortArrsize * 2];
for (int i = 0; i < shortArrsize; i++) {
bytes[i * 2] = (byte) (sData[i] & 0x00FF);
bytes[(i * 2) + 1] = (byte) (sData[i] >> 8);
sData[i] = 0;
}
return bytes;
}
private void writeAudioDataToFile() {
// Write the output audio in byte
String filePath = "/sdcard/MyApp/8k16bitMono.pcm";
short sData[] = new short[BufferElements2Rec];
FileOutputStream os = null;
try {
os = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (isRecording) {
// gets the voice output from microphone to byte format
recorder.read(sData, 0, BufferElements2Rec);
System.out.println("Short writing to file" + sData.toString());
try {
// writes the data to file from buffer stores the voice buffer
byte bData[] = short2byte(sData);
Log.v(TAG,"Am I here??"); // Here is my log!!
os.write(bData, 0, BufferElements2Rec * BytesPerElement);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording() {
// stops the recording activity
if (null != recorder) {
isRecording = false;
recorder.stop();
recorder.release();
recorder = null;
recordingThread = null;
}
}
private View.OnClickListener btnClick = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart: {
enableButtons(true);
startRecording();
break;
}
case R.id.btnStop: {
enableButtons(false);
stopRecording();
break;
}
}
}
};
// onClick of backbutton finishes the activity.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.onKeyDown(keyCode, event);
}
}
采纳答案by laalto
@Override
Log.e(TAG,"I shouldn't be here");
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Method calls such as Log.e()
should be in a method body. They cannot be directly in the class body. So, change it to something like this:
方法调用,例如Log.e()
应该在方法体中。它们不能直接在类主体中。所以,把它改成这样:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG,"I shouldn't be here");
回答by S.Thiongane
Use it like this : Log.e("YOUR LOG TAG", "YOUR LOG MESSAGE");
像这样使用它: Log.e("YOUR LOG TAG", "YOUR LOG MESSAGE");
回答by Dmitrijs
first, import Log ( above your class( not in it)) = >
首先,导入日志(在您的班级之上(不在其中))= >
import android.util.Log;
make your tag to find your Log in debug window (field in your class) =>
让您的标签找到您的登录调试窗口(您班级中的字段)=>
public static final String LOG_TAG = "myLogs";
and you need 2 parameters your tag + your String ( put in your method )=>
你需要2个参数你的标签+你的字符串(放在你的方法中)=>
Log.d(LOG_TAG, "I shouldn't be here");
Log.v();
// Verbose
Log.d();
// Debug
Log.i();
// Info
Log.w();
// Warning
Log.e();
// Error
Log.v();
// 详细
Log.d();
// 调试
Log.i();
// 信息
Log.w();
// 警告
Log.e();
// 错误
回答by nsgulliver
If you see the Log class Page, do you find any Log.e
with one parameter? You must add another parameter in order to make it work.
如果您看到Log 类 Page,您是否发现任何Log.e
带有一个参数的类?您必须添加另一个参数才能使其工作。
Log.e("SOMETAG","I shouldn't be here");
回答by istovatis
You should add a String that refers to this Log error first.
您应该首先添加一个引用此日志错误的字符串。
For example create a String named TAG and add it to your statement
例如,创建一个名为 TAG 的字符串并将其添加到您的语句中
private static final String TAG = "Your Tag";
Log.e(TAG, "I shouldn't be here");
回答by androidcodehunter
The original format of the Log.e is like the below code:
Log.e 的原始格式类似于以下代码:
Log.e(String tag,String msg)
So you have to pass two String
as a parameter. So if you want to show something as a error, you can write it like this:
所以你必须传递两个String
作为参数。因此,如果您想将某些内容显示为错误,可以这样编写:
Log.e("TAG","I shouldn't be here");
Hope it will works fine. You can also add a global tag for error like this way
希望它能正常工作。您还可以像这样为错误添加全局标记
public static final String TAG = "MY_TAG";
Log.e(TAG, "I shouldn't be here");
回答by ray
You can use Log.e("TAG", "Msg")
or Log.d("TAG", "Msg");
您可以使用Log.e("TAG", "Msg")
或Log.d("TAG", "Msg");
TAG
is any String
that will show you in TID of LogCat. Suppose Log.e("UserID", userId);
TAG
是任何String
将在 LogCat 的 TID 中显示的内容。认为Log.e("UserID", userId);
Don't forget to import: import android.util.Log;
不要忘记导入: import android.util.Log;
回答by rahul sondarva
Use it like this
像这样使用它
String p="rahul";
Log.e("SOMETAG","I shouldn't be here"+p);
回答by Yo Apps
I can give you a tip from my experience to make it more efficient.Besides the regular...
我可以根据我的经验给你一个提示,让它更有效率。除了常规......
private static final String TAG = "Activity OR Class";
Log.e(TAG, "Value of variable: " + variable);
I also advice you to put the location of the code you are in. For eg. lets say you have a variable count, that is used in onCreate()as well as anotherMethod()and you want to log its value.
我还建议你把你所在的代码的位置。例如。假设您有一个变量count,它在onCreate()和anotherMethod() 中使用,并且您想记录其值。
Log.e(TAG, "onCreate() value of count: " + count);
Log.e(TAG, "anotherMethod() value of count: " + count);
So, besides getting the TAG(class OR activity) this came from, you will also get the method() OR position that logged this!
因此,除了获得它来自的TAG(类 OR 活动)之外,您还将获得记录此信息的 method() OR 位置!