3

When I Use Lambda like this it report that

Cannot resolve method getAction()

Code

BroadcastReceiver refreshDataReceiver = (context,intent)-> {
        if (AppConstants.REFRESH_DATA_ACTION.equals(intent.getAction())) {
            taskInfos.clear();
            taskInfos.addAll(mTaskDao.queryMyTasks());
            mAdapter.notifyDataSetChanged();
        }
};

While I write this code in normal way, it works well ,why?

 BroadcastReceiver refreshDataReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (AppConstants.REFRESH_DATA_ACTION.equals(intent
                .getAction())) {
            taskInfos.clear();
            taskInfos.addAll(mTaskDao.queryMyTasks());
            mAdapter.notifyDataSetChanged();
        }
    }
};
tarzanbappa
  • 4,930
  • 22
  • 75
  • 117
Ryan
  • 91
  • 7
  • What version of Java are you using. See: http://stackoverflow.com/questions/23318109/is-it-possible-to-use-java-8-for-android-development – Morrison Chang Dec 15 '15 at 03:44
  • Java 8 bt_opt.setOnClickListener((v)-> { Intent intent = new Intent(getActivity(), MyTaskMapActivity.class); intent.putParcelableArrayListExtra("tasks", (ArrayList extends Parcelable>) taskInfos); getActivity().startActivity(intent); }); – Ryan Dec 15 '15 at 04:01

2 Answers2

3

BroadcastReceiver is not a valid candidate for lambda replacement. Lambdas can only replace single method interfaces. From the Java Lambda Quickstart docs --

Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression.

BroadcastReceiver is neither an interface nor does it have just a single method.

iagreen
  • 31,470
  • 8
  • 76
  • 90
1

Add Retrolambda to your Gradle build configuration

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.4.0'
    classpath 'me.tatarka:gradle-retrolambda:3.2.0'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

Add the source and target compatibility to Java 8 and apply the new plug-in in your app/build.gradle file.

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc2"

defaultConfig {
    applicationId "com.vogella.android.retrolambda"
    minSdkVersion 22
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
}



dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
} 

then use lambda expressions. link

Vishal Raj
  • 1,755
  • 2
  • 16
  • 35