【问题标题】:Cannot resolve method (<lambda expresion>)无法解析方法(<lambda 表达式>)
【发布时间】:2018-07-24 05:32:06
【问题描述】:

您好,我正在尝试使用 https://github.com/iammert/ExpandableLayout 制作 ExpandableLayout。但是这行代码不断出错`

 @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                sectionLinearLayout.filterChildren(obj -> ((GradIndjija) obj).name.toLowerCase().contains(s.toString().toLowerCase()));
            }

`

我在 filterChildren 上收到“无法解析方法 filterChildren ()”和在 ((GradIndjija) obj) 上无法投射的消息。 我的代码:`

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import iammert.com.expandablelib.ExpandCollapseListener;
import iammert.com.expandablelib.ExpandableLayout;
import iammert.com.expandablelib.Section;




public class Ogradu extends AppCompatActivity {

    String[] parents = new String[]{"O Indjiji",
            "Istorija Indjije", "Kultura"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.o_gradu);


        EditText editText = (EditText) findViewById(R.id.edittext);
        ExpandableLayout sectionLinearLayout = (ExpandableLayout) findViewById(R.id.el);

        sectionLinearLayout.setRenderer(new ExpandableLayout.Renderer<KategorijaGrada, GradIndjija>() {
            @Override
            public void renderParent(View view, KategorijaGrada model, boolean isExpanded, int parentPosition) {
                ((TextView) view.findViewById(R.id.tv_parent_name)).setText(model.naziv);
                view.findViewById(R.id.arrow).setBackgroundResource(isExpanded ? R.drawable.ic_arrow_up : R.drawable.ic_arrow_down);
            }

            @Override
            public void renderChild(View view, GradIndjija model, int parentPosition, int childPosition) {
                ((TextView) view.findViewById(R.id.tv_child_name)).setText(model.naziv);
            }
        });

        sectionLinearLayout.addSection(getSection());
        sectionLinearLayout.addSection(getSection());
        sectionLinearLayout.addSection(getSection());
        sectionLinearLayout.addSection(getSection());
        sectionLinearLayout.addSection(getSection());
        sectionLinearLayout.addSection(getSection());
        sectionLinearLayout.addSection(getSection());
        sectionLinearLayout.addSection(getSection());
        sectionLinearLayout.addSection(getSection());
        sectionLinearLayout.addSection(getSection());
        sectionLinearLayout.addSection(getSection());

        sectionLinearLayout.setExpandListener((ExpandCollapseListener.ExpandListener<KategorijaGrada>) (parentIndex, parent, view) -> {

        });

        sectionLinearLayout.setCollapseListener((ExpandCollapseListener.CollapseListener<KategorijaGrada>) (parentIndex, parent, view) -> {

        });


        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                sectionLinearLayout.filterChildren(obj -> ((GradIndjija) obj).name.toLowerCase().contains(s.toString().toLowerCase()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public Section<KategorijaGrada, GradIndjija> getSection() {
        Section<KategorijaGrada, GradIndjija> section = new Section<>();
        KategorijaGrada kategorijeGrada = new KategorijaGrada(parents[(int) (Math.random() * parents.length)]);
        GradIndjija grad1 = new GradIndjija("Istorija Grada");
        GradIndjija grad2 = new GradIndjija("O Indjiji");
        GradIndjija grad3 = new GradIndjija("Kultura grada");

        section.parent = kategorijeGrada;
        section.children.add(grad1);
        section.children.add(grad2);
        section.children.add(grad3);
        section.expanded = true;
        return section;
    }
}

`

我的 gradle 文件:`

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.korisnik.indjijavodic"
        minSdkVersion  15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //biblioteka
    compile 'com.github.iammert:ExpandableLayout:1.4.1'

}

` 我的 logcat 中没有收到任何错误消息,因为应用程序甚至还没有启动。我的消息 gradle build 中只有错误:

    C:\Users\Korisnik\Downloads\ud839_Miwok-Starter-code\IndjijaVodic\app\src\main\java\com\example\korisnik\indjijavodic\Ogradu.java
Error:(75, 36) error: cannot find symbol method filterChildren((obj)->((G[...]se()))
Error:(75, 78) error: cannot find symbol variable name
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

GradIndjija.java:`

    package com.example.korisnik.indjijavodic;

    public class GradIndjija {
       public String kategorija;

        public GradIndjija(String naziv) {
            this.naziv = naziv;
        }
    }

`

and my second class KategorijaGrada:`package com.example.korisnik.indjijavodic;

/**
 * Created by Korisnik on 23.7.2018..
 */

public class KategorijaGrada {
    public String naziv;

    public KategorijaGrada(String naziv) {
        this.naziv = naziv;
    }
}

`

我昨天一整天都在谷歌上搜索并尝试了我所知道的所有内容,尽管我尝试的所有内容并不多,所以任何帮助都更重要

【问题讨论】:

  • 一切似乎都是正确的。你能粘贴你的日志和你的 gradle 文件吗?
  • @maheryhaja 在这里我编辑了我的问题和 pu gradle 文件..非常感谢... :)
  • 你能粘贴你的 GradIndjija 课程吗?问题可能来自那里
  • 已编辑,放入两个类

标签: java android


【解决方案1】:

您无法编译,因为字段 name 未定义。

首先看起来GradIndjija类应该是这样的:

public class GradIndjija {
   // naziv instead of kategorija
   public String nziv;

    public GradIndjija(String naziv) {
        this.naziv = naziv;
    }
}

过滤器函数应该是这样的:

   public void onTextChanged(CharSequence s, int start, int before, int count) {
      //naziv instead of name
      sectionLinearLayout.filterChildren(obj -> ((GradIndjija) obj).naziv.toLowerCase().contains(s.toString().toLowerCase()));
   }

希望它会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2018-03-04
    • 1970-01-01
    • 2016-01-21
    • 2017-03-28
    • 2021-09-16
    相关资源
    最近更新 更多