【问题标题】:Incompatibele types error when copying code复制代码时出现不兼容类型错误
【发布时间】:2019-04-01 11:35:21
【问题描述】:

我正在一个程序中设置一个记录器,但使用了另一个程序的一些代码。除了由 Collectors.toList() 返回的不兼容类型错误之外,我可以修复所有错误。

错误: https://i.imgur.com/ak5pCEb.png

我得到的回报: https://i.imgur.com/Ox6aCjw.png

我需要的回报: https://i.imgur.com/JlLNPvz.png

列表应该返回一个字符串数组,但此时只返回一个对象数组。我不能将其转换为字符串数组,也不能将 mLines 数组更改为对象数组,因为这会在后面的代码中引起问题。

这些函数背后的代码是本机 java,因此更改它会在我的计算机上随处更改。知道如何解决这个问题吗?

private static class LogcatAdapter extends BaseAdapter {

        private List<String> mLines = Collections.emptyList();

        void reload() {
            try {
                Process process = Runtime.getRuntime().exec("logcat -d -t 500 -v time");
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                mLines = reader.lines().collect(Collectors.toList());
            } catch (IOException e) {
                StringWriter str = new StringWriter();
                e.printStackTrace(new PrintWriter(str));
                mLines = Collections.singletonList(str.toString());
            }
            notifyDataSetChanged();
        }

        @Override
        public String toString() {
            return BuildConfig.APPLICATION_ID + " " + BuildConfig.VERSION_NAME + "\n" +
                   mLines.stream().collect(Collectors.joining("\n"));
        }

        @Override
        public int getCount() {
            return mLines.size();
        }

        @Override
        public String getItem(int position) {
            return mLines.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            if (view == null) {
                view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_logline, parent, false);
            }
            String line = getItem(position);
            ((TextView) view.findViewById(R.id.text)).setText(line);
            return view;
        }

    }

【问题讨论】:

  • 请不要添加错误截图

标签: java android logging tolist


【解决方案1】:

您可以“手动”指定toList 调用的T:

mLines = reader.lines().collect(Collectors.<String>toList());

但是应该没有必要。这似乎是 IntelliJ 中的一个错误,代码实际上可以编译并运行良好。

【讨论】:

    猜你喜欢
    • 2013-09-14
    • 1970-01-01
    • 2016-04-18
    • 2018-03-05
    • 2022-01-26
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多