【问题标题】:Logcat Doesn't work properly. why?Logcat 无法正常工作。为什么?
【发布时间】:2018-04-12 16:15:25
【问题描述】:

Logcat 只出现在MainActivityonCreate 方法内部。我试图在一个类中使用Log (TAG, "msg");,但没有出现日志。为什么?在MainActivity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.v("Test", "test");
}

有效!

但是这个类不起作用:

public class Imgpixel {
    private static final String TAG = "Quicknotes";

    public Imgpixel() {

    }
    String src="C:\\path_of_image\\img.jpg";
    Mat imgRead = Imgcodecs.imread(src, IMREAD_COLOR);

    int lin = imgRead.rows(); //get the number of rows
    int col = imgRead.cols(); //get the number of cols

    List<double[]> pixels=new ArrayList<>();//arraylist to save array rgb below

    public void cor() {
        for (int i = 0; i < lin; i++) {
            for (int j = 0; j < col; j++) {
                double [] rgb = imgRead.get(i, j);
                pixels.add(0, rgb);
            }
        }
    }
}

【问题讨论】:

  • "但在另一个班级不工作"请张贴minimal reproducible example
  • 能确认方法执行了吗?尝试在有问题的行上放置一个断点。
  • 我编辑了上面的代码。
  • 能否贴出负责调用“cor()”方法的代码?
  • 另外,Log.v("TAG", "test") 会在 logcat 中为您提供“TAG”,而不是我希望您真正想要的“Quicknotes”。

标签: java android logcat android-studio-3.0


【解决方案1】:

我很惊讶您的代码示例会编译。在您的 Imgpixel 类中,方法 cor() 在类外部定义,变量 pixels 将无法访问,并且您有一个额外的 }

像这样从另一个类(例如,从MainActivity 内部)调用您的cor 方法:

Imgpixel imgPixel = new Imgpixel();
imgPixel.cor();

当你的 Imgpixel 类看起来像这样时:

 public class Imgpixel{
    private static final String TAG = "Quicknotes";

    String src="C:\\path_of_image\\img.jpg";
    Mat imgRead = Imgcodecs.imread(src, IMREAD_COLOR);

    int lin = imgRead.rows(); //get the number of rows
    int col = imgRead.cols(); //get the number of cols

    List<double[]> pixels=new ArrayList<>();

    public Imgpixel(){
        // initializing
    }

    public void cor() {
        Log.v(TAG, "test"); //Remove the quotation marks from TAG in order to get value from the variable you set earlier
        for (int i = 0; i < lin; i++) {
            for (int j = 0; j < col; j++) {
                double [] rgb = imgRead.get(i, j);
                pixels.add(0, rgb);
            }
        }
    }

} //this marks the end of the Imgpixel class

但您也可以将Imgpixel 设为静态类。

【讨论】:

  • 我可能复制了错误的“{”。在我的代码中是正确的,“cor()”方法在类内。
  • 另一个问题,我想可视化数组列表“像素”中保存的内容?有什么办法在android上做到这一点?
  • 我创建了类对象并像这样调用 color() 方法: public class Imgpixel { // 变量 } public Imgpixel () { Imgpixel imgpixel = new Imgpixel (); imgpixel.cor (); } public void color () { // 方法 }
  • 我的日志没有出现。只出现在 onCreate 方法内部,为什么?
  • 我不知道它是否适合我。在 logcat 中显示日志没问题。
猜你喜欢
  • 2011-07-24
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-03
  • 2023-03-22
  • 1970-01-01
相关资源
最近更新 更多