mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
fix(android): add null check for textInputPlugin in FlutterView (#180386)
Prevents NPE in onProvideAutofillVirtualStructure when attachToEngineAutomatically is false and autofill is triggered before manual engine attachment. Added null safety check with early return and debug warning. Maintains backward compatibility. Fixes: flutter/flutter/issues/149792、https://github.com/flutter/flutter/issues/180383 <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> *Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.* *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
parent
b7b2ec7eda
commit
ed1922e4fa
@ -1594,12 +1594,20 @@ public class FlutterView extends FrameLayout
|
||||
@Override
|
||||
public void onProvideAutofillVirtualStructure(@NonNull ViewStructure structure, int flags) {
|
||||
super.onProvideAutofillVirtualStructure(structure, flags);
|
||||
textInputPlugin.onProvideAutofillVirtualStructure(structure, flags);
|
||||
// Defensive null check to prevent NPE when textInputPlugin is not yet initialized
|
||||
// (e.g., when attachToEngineAutomatically is false).
|
||||
if (textInputPlugin != null) {
|
||||
textInputPlugin.onProvideAutofillVirtualStructure(structure, flags);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void autofill(@NonNull SparseArray<AutofillValue> values) {
|
||||
textInputPlugin.autofill(values);
|
||||
// Defensive null check to prevent NPE when textInputPlugin is not yet initialized
|
||||
// (e.g., when attachToEngineAutomatically is false).
|
||||
if (textInputPlugin != null) {
|
||||
textInputPlugin.autofill(values);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -39,11 +39,14 @@ import android.media.ImageReader;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.SparseArray;
|
||||
import android.view.DisplayCutout;
|
||||
import android.view.RoundedCorner;
|
||||
import android.view.Surface;
|
||||
import android.view.View;
|
||||
import android.view.ViewStructure;
|
||||
import android.view.WindowInsets;
|
||||
import android.view.autofill.AutofillValue;
|
||||
import android.widget.FrameLayout;
|
||||
import androidx.core.util.Consumer;
|
||||
import androidx.test.core.app.ActivityScenario;
|
||||
@ -59,6 +62,7 @@ import io.flutter.embedding.engine.renderer.FlutterRenderer;
|
||||
import io.flutter.embedding.engine.systemchannels.SettingsChannel;
|
||||
import io.flutter.plugin.platform.PlatformViewsController;
|
||||
import io.flutter.plugin.platform.PlatformViewsController2;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@ -1464,4 +1468,32 @@ public class FlutterViewTest {
|
||||
return View.SYSTEM_UI_FLAG_FULLSCREEN;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that autofill methods do nothing when TextInputPlugin is null. This verifies that no
|
||||
* NullPointerException is thrown and the plugin methods are not called.
|
||||
*/
|
||||
@Test
|
||||
public void autofill_doesNothingWhenTextInputPluginIsNull() throws Exception {
|
||||
// Setup: Create FlutterView without attaching to engine
|
||||
// This simulates the case where textInputPlugin is null (e.g., when
|
||||
// attachToEngineAutomatically is false)
|
||||
FlutterView flutterView = new FlutterView(ctx);
|
||||
|
||||
// Verify textInputPlugin is null (not initialized)
|
||||
Field textInputPluginField = FlutterView.class.getDeclaredField("textInputPlugin");
|
||||
textInputPluginField.setAccessible(true);
|
||||
assertNull(textInputPluginField.get(flutterView));
|
||||
|
||||
// Test onProvideAutofillVirtualStructure - should not throw NPE
|
||||
ViewStructure structure = mock(ViewStructure.class);
|
||||
int flags = 0;
|
||||
flutterView.onProvideAutofillVirtualStructure(structure, flags);
|
||||
// No exception should be thrown
|
||||
|
||||
// Test autofill - should not throw NPE
|
||||
SparseArray<AutofillValue> values = mock(SparseArray.class);
|
||||
flutterView.autofill(values);
|
||||
// No exception should be thrown
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user