Java Android中的Log.e

Java Android中的Log.e,java,android,logging,Java,Android,Logging,我正在尝试登录我的程序,以便它帮助我调试,但Log语句本身给出了一个错误。我这样写日志消息 import android.util.Log; public static final String TAG = "MyActivity"; Log.e(TAG,"I shouldn't be here"); public static final String TAG = "MY_TAG"; Log.e(TAG, "I shouldn't be here"); 这是我在公开课上发表的声明。它给出了

我正在尝试登录我的程序,以便它帮助我调试,但Log语句本身给出了一个错误。我这样写日志消息

import android.util.Log;
public static final String TAG = "MyActivity";
Log.e(TAG,"I shouldn't be here");
public static final String TAG = "MY_TAG";
Log.e(TAG, "I shouldn't be here");
这是我在公开课上发表的声明。它给出了错误:

1. Syntax error on token "(", delete this token.
2. Syntax error on token, Variable Declarator Expected instead.
我不熟悉android SDK开发和Java,因此任何帮助都将不胜感激

谢谢

我在这里输入了我的确切代码:

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);
 }
}
package com.android.record;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入android.app.Activity;
导入android.media.AudioFormat;
导入android.media.AudioRecord;
导入android.media.MediaRecorder;
导入android.os.Bundle;
导入android.view.KeyEvent;
导入android.view.view;
导入android.widget.Button;
导入android.util.Log;
//导入com.android.record.R;
公共类录音测试扩展活动{
专用静态最终积分记录仪\u采样器=8000;
专用静态最终整数记录器\u通道=单声道中的AudioFormat.CHANNEL\u;
专用静态最终整数记录器\u音频\u编码=AudioFormat.ENCODING\u PCM\u 16位;
专用录音机=空;
私有线程recordingThread=null;
私有布尔值isRecording=false;
公共静态最终字符串TAG=“MyActivity”;
@凌驾
Log.e(标记“我不应该在这里”);
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u audio\u record\u test);
setButtonHandlers();
启用按钮(错误);
int bufferSize=AudioRecord.getMinBufferSize(记录器\u采样器,
录像机信道、录像机音频编码);
}
私有void setButtonHandlers(){
((按钮)findviewbyd(R.id.btnStart)).setOnClickListener(btnClick);
((按钮)findviewbyd(R.id.btnStop)).setOnClickListener(btnClick);
}
私有void enableButton(int-id,boolean-isEnable){
((按钮)findViewById(id)).setEnabled(isEnable);
}
专用void启用按钮(布尔值isRecording){
启用按钮(R.id.btnStart,!isRecording);
启用按钮(R.id.btnStop,isRecording);
}
int BufferElements2Rec=1024;//要播放2048(2K),因为我们只使用1024个字节
int bytesperement=2;//16位格式的2个字节
私有无效开始记录(){
录音机=新的录音机(MediaRecorder.AudioSource.MIC,
记录仪采样器、记录仪通道、,
记录器\音频\编码,缓冲元素2REC*BytesPerElement);
记录器。开始记录();
isRecording=true;
recordingThread=新线程(new Runnable()){
公开募捐{
WriteeAudioDataToFile();
}
}“录音机线程”);
recordingThread.start();
}
//从短到字节的转换
专用字节[]短2字节(短[]sData){
int shortArrsize=sData.length;
字节[]字节=新字节[shortArrsize*2];
对于(int i=0;i>8);
sData[i]=0;
}
返回字节;
}
私有void writeeAudioDataToFile(){
//以字节写入输出音频
字符串filePath=“/sdcard/MyApp/8k16bitMono.pcm”;
短sData[]=新短[BufferElements2Rec];
FileOutputStream os=null;
试一试{
os=新的FileOutputStream(filePath);
}catch(filenotfounde异常){
e、 printStackTrace();
}
while(isRecording){
//获取从麦克风到字节格式的语音输出
记录器读取(sData,0,缓冲元件2REC);
System.out.println(“短写入文件”+sData.toString());
试一试{
//将数据从缓冲区写入文件存储语音缓冲区
字节bData[]=short2byte(sData);
Log.v(标记“我在这里吗?”;//这是我的日志!!
写入(bData,0,BufferElements2Rec*bytesperement);
}捕获(IOE异常){
e、 printStackTrace();
}
}
试一试{
os.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
私有void stopRecording(){
//停止录制活动
如果(空!=记录器){
isRecording=false;
录音机。停止();
记录器。释放();
记录器=空;
recordingThread=null;
}
}
private View.OnClickListener btnClick=new View.OnClickListener(){
公共void onClick(视图v){
开关(v.getId()){
案例R.id.btnStart:{
启用按钮(真);
startRecording();
打破
}
案例R.id.btnStop:{
启用按钮(错误);
停止录制();
打破
}
}
}
};
//单击backbutton完成活动。
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件){
if(keyCode==KeyEvent.keyCode\u BACK){
完成();
}
返回super.onKeyDown(keyCode,event);
}
}

像这样使用:
Log.e(“您的日志标签”、“您的日志消息”)

首先,导入日志(在类上方(不在其中))=>

标记以在调试窗口中查找日志(类中的字段)=>

您需要2个参数—标记+字符串(放入方法)=>

Log.v()//详细
Log.d()//调试
Log.i()//信息
Log.w()//警告
Log.e()
//Error

如果您看到,是否找到带有一个参数的
Log.e
?必须添加另一个参数才能使其工作

Log.e("SOMETAG","I shouldn't be here");

您应该首先添加一个引用此日志错误的字符串

例如,创建一个名为TAG的字符串并将其添加到语句中

private static final String TAG = "Your Tag";
Log.e(TAG, "I shouldn't be here"); 

Log.e的原始格式如下所示:

Log.e(String tag,String msg)
因此,必须将两个
字符串作为参数传递。因此,如果要将某个内容显示为错误,可以这样编写:

Log.e("TAG","I shouldn't be here");
希望它能正常工作。 您还可以像这样为错误添加全局标记

import android.util.Log;
public static final String TAG = "MyActivity";
Log.e(TAG,"I shouldn't be here");
public static final String TAG = "MY_TAG";
Log.e(TAG, "I shouldn't be here");
您可以使用
Log.e(“TAG”、“Msg”)
Log.d(“TAG”、“Msg”)

标记
是任何将在LogCat的TID中显示的
字符串
。假设
Log.e(“UserID”,UserID)

别忘了导入:
import android.util.Log

@Override
Log.e(TAG,"I shouldn't be here");  
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
方法调用,例如
Log.e()<
@Override
Log.e(TAG,"I shouldn't be here");  
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Log.e(TAG,"I shouldn't be here");  
String p="rahul";
Log.e("SOMETAG","I shouldn't be here"+p);
private static final String TAG = "Activity OR Class";
Log.e(TAG, "Value of variable: " + variable); 
Log.e(TAG, "onCreate() value of count: " + count);
Log.e(TAG, "anotherMethod() value of count: " + count);