gpt4 book ai didi

android - Robolectric 无法膨胀 DialogFragment 中的 View

转载 作者:行者123 更新时间:2023-11-29 21:56:05 37 4
gpt4 key购买 nike

我正在尝试测试 DialogFragment 的以下方法:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View rootView = View.inflate(getActivity(), R.layout.fragment_fileproperties_layout, null);
thumbImageView = (ImageView) rootView.findViewById(R.id.filePropertiesThumb);
nameTextView = (TextView) rootView.findViewById(R.id.filePropertiesName);
typeTextView = (TextView) rootView.findViewById(R.id.filePropertiesTypeValue);
locationTextView = (TextView) rootView.findViewById(R.id.filePropertiesLocationValue);
sizeTextView = (TextView) rootView.findViewById(R.id.filePropertiesSizeValue);
numberOfFilesLayout = (LinearLayout) rootView
.findViewById(R.id.filePropertiesNumOfFilesLayout);
numberOfFilesTextView = (TextView) rootView
.findViewById(R.id.filePropertiesNumOfFilesValue);
lastModifiedLayout = (LinearLayout) rootView
.findViewById(R.id.filePropertiesLastModifiedLayout);
lastModifiedTextView = (TextView) rootView
.findViewById(R.id.filePropertiesLastModifiedValue);
hiddenCheckbox = (CheckBox) rootView.findViewById(R.id.filePropertiesHiddenCheckbox);
hiddenCheckbox.setOnCheckedChangeListener(this);
typeCategoryTextView = (TextView) rootView.findViewById(R.id.filePropertiesCategoryType);
infoCategoryTextView = (TextView) rootView.findViewById(R.id.filePropertiesCategoryInfo);
attributesCategoryTextView = (TextView) rootView
.findViewById(R.id.filePropertiesCategoryAttributes);
populateViews();
if (selectedFiles.size() == 1) {
if (selectedFiles.get(0).isFile()) {
populateViewsFile();
} else {
populateViewsFolder();
}
} else {
populateViewsMultiple();
}
toggleHiddenOnExit = false;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(rootView).setTitle(R.string.properties)
.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, this);
return builder.create();
}

populateViews() 方法如下所示:

private void populateViews() {
final int backgroundColor = getResources().getColor(android.R.color.darker_gray);
final int textColor = getResources().getColor(android.R.color.black);
typeCategoryTextView.setText(R.string.type);
typeCategoryTextView.setBackgroundColor(backgroundColor);
typeCategoryTextView.setTextColor(textColor);
infoCategoryTextView.setText(R.string.info);
infoCategoryTextView.setBackgroundColor(backgroundColor);
infoCategoryTextView.setTextColor(textColor);
attributesCategoryTextView.setText(R.string.attributes);
attributesCategoryTextView.setBackgroundColor(backgroundColor);
attributesCategoryTextView.setTextColor(textColor);
}

现在,在测试方法中看起来像这样:

@Test
public void testCreateDialogForAFile() {
file = new File(Environment.getExternalStorageDirectory(), "/file.txt");
try {
file.createNewFile();
} catch (IOException e) {
LogUtils.e(e.getMessage(), e);
}
List<File> files = new ArrayList<File>();
files.add(file);
dialog.setSelectedFiles(files);
assertNotNull("onCreateDialog not null", dialog.onCreateDialog(null));
file.delete();
}

这里是异常(exception):

java.lang.NullPointerException
at com.foo.bar.fragment.dialog.FooDialogFragment.onCreateDialog(FooDialogFragment.java:93)
at com.foo.bar.fragment.dialog.FooDialogFragmentTest.testCreateDialogForAFile(FooDialogFragmentTest.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:292)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

Views 没有膨胀,测试失败。这个问题有什么解决方法吗?

最佳答案

原因是ShadowDialogFragment在 Robolectric 中未正确实现。实现了 show() 方法,但没有 onCreateDialog()。一种解决方案是自己实现并推送到存储库。

编辑:

我已阅读您的评论,并仔细查看了来源。该对话框是在阴影实现的 show() 方法中创建和附加的。

在测试 View 之前尝试调用 show():

dialogFragment.show(fragmentManager, "tag");
assertNotNull("dialog not null", dialogFragment.getDialog());

That将帮助您使用 FragmentManager

关于android - Robolectric 无法膨胀 DialogFragment 中的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13228540/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com