com.***.databinding不存在

本文详细解析DataBinding中常见的三个问题:android:text属性中避免使用中文,整个TextView内不可包含中文,变量名避免使用下划线。并提供了解决方案,如通过string.xml资源文件进行字符串拼接,实体类内部处理,以及正确的变量命名规范。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DataBinding的恶心bug

  • 程序包不存在 一般都是xml文件出错

DataBinding的布局文件中不允许使用中文

 

1、android:text 不允许出现中文 或中文 字符

android:text='@{String.valueOf("姓名:"+user.name)}' 

把需要拼接的定义到string.xml了 


<string name="label_rmb">¥</string> 

 

然后调用时就像这样

android:text="@{@string/label_rmb+user.name}" 

//或者 


android:text="@{String.format(@string/label_rmb_2,user.name)}" 


另外还有一种方式,那就是在实体类里面处理
 

public class User { 
    public String name; 
    public String des; 
    public String getNameStr() { 
        return "姓名:" + name; 
    } 
} 


然后调用时就像这样

android:text="@{user.nameStr}" 

2、整个TextView里面也不许有中文

 

<TextView
    android:tag="@string/SBXH"
    android:onClick="@{click.onClick}"
    android:text="@{table.SBXH}"
    android:id="@+id/tv_SBXH"

    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/table_tv_click"
    android:gravity="center"
    android:padding="8dp"
    android:textSize="20sp" />

 

3、name取名 不要存在 _

 

<variable name="table" type="Table_1"/> 

apply plugin: 'jacoco' //gradlew clean & gradlew createOfflineTestCoverageReport & gradlew jacocoTestReport tasks.withType(Test) { jacoco.includeNoLocationClasses = true jacoco.excludes = ['jdk.internal.*'] } configurations { jacocoAnt jacocoRuntime } jacoco { toolVersion = "0.8.5" } def offline_instrumented_outputDir = "$buildDir.path/intermediates/classes-instrumented/debugCoverage" tasks.withType(Test) { jacoco.includeNoLocationClasses = true jacoco.excludes = ['jdk.internal.*'] } def coverageSourceDirs = [ 'src/main/java' ] task jacocoTestReport(type: JacocoReport, dependsOn: "test") { group = "Reporting" description = "Generate Jacoco coverage reports" classDirectories.from = fileTree( dir: './build/intermediates/javac/debugCoverage/classes/', excludes: [ '**/R.class', '**/R$*.class', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*', '**/BuildConfig.*', '**/**Bean.class', '**/inputmethod/*', '**/config/*.*', '**/flex/*.*', '**/AssetsUtils.class', '**/OkHttpHelper.java', '**/StableFocusRecyclerView.*', '**/databinding/**', '**/adaptor/**', '**/base/utils/**', '**/ui/activity/**', '**/ui/dialog/**', '**/ui/fragment/**', '**/ui/widget/**', ] ) getSourceDirectories().setFrom(files(coverageSourceDirs)) getExecutionData().setFrom(files('./build/jacoco/testDebugUnitTest.exec')) } jacocoTestReport { reports { xml.enabled true html.enabled true html.destination file("build/test-results/jacocoHtml") } } /* This task is used to create offline instrumentation of classes for on-the-fly instrumentation coverage tool like Jacoco. See jacoco classId * and Offline Instrumentation from the jacoco site for more info. * * In this case, some classes mocked using PowerMock were reported as 0% coverage on jacoco & Sonarqube. The issue between PowerMock and jacoco * is well documented, and a possible solution is offline Instrumentation (not so well documented for gradle). * * In a nutshell, this task: * - Pre-instruments the original *.class files * - Puts the instrumented classes path at the beginning of the task's classpath (for report purposes) * - Runs test & generates a new exec file based on the pre-instrumented classes -- as opposed to on-the-fly instrumented class files generated by jacoco. * * It is currently not implemented to run prior to any other existing tasks (like test, jacocoTestReport, etc...), therefore, it should be called * explicitly if Offline Instrumentation report is needed. * * Usage: gradle clean & gradle createOfflineInstrTestCoverageReport & gradle jacocoTestReport * - gradle clean //To prevent influence from any previous task execution * - gradle createOfflineInstrTestCoverageReport //To generate *.exec file from offline instrumented class * - gradle jacocoTestReport //To generate html report from newly created *.exec task */ task createOfflineTestCoverageReport(dependsOn: ['instrument', 'testDebugCoverageUnitTest']) { group = "reporting" doLast { ant.taskdef(name: 'report', classname: 'org.jacoco.ant.ReportTask', classpath: configur3+ations.jacocoAnt.asPath) ant.report() { executiondata { ant.file(file: "$buildDir.path/jacoco/testDebugCoverageUnitTest.exec") } structure(name: 'Example') { classfiles { fileset(dir: "$project.buildDir/intermediates/javac/debugCoverage") } sourcefiles { fileset(dir: 'src/main/java') } } //Uncomment if we want the task to generate jacoco html reports. However, the current script does not exclude files. //An alternative is to used jacocoTestReport after this task finishes //html(destdir: "$buildDir.path/reports/jacocoHtml") } } } createOfflineTestCoverageReport.dependsOn(clean) /* * Part of the Offline Instrumentation process is to add the jacoco runtime to the class path along with the path of the instrumented files. */ gradle.taskGraph.whenReady { graph -> if (graph.hasTask(instrument)) { tasks.withType(Test) { doFirst { systemProperty 'jacoco-agent.destfile', buildDir.path + '/jacoco/testDebugCoverageUnitTest.exec' classpath = files(offline_instrumented_outputDir) + classpath + configurations.jacocoRuntime } } } } /* * Instruments the classes per se */ task instrument(dependsOn:'compileDebugUnitTestSources') { doLast { println 'Instrumenting classes' ant.taskdef(name: 'instrument', classname: 'org.jacoco.ant.InstrumentTask', classpath: configurations.jacocoAnt.asPath) ant.instrument(destdir: offline_instrumented_outputDir) { fileset(dir: "$buildDir.path/intermediates/javac/debugCoverage") } } } ========================================== package com.st.systemsettings.presenter; import android.content.Context; import android.net.NetworkInfo; import com.st.systemsettings.R; import com.st.systemsettings.adapter.SystemAdapter; import com.st.systemsettings.bean.SystemBean; import com.st.systemsettings.bean.SystemSoftwareInfo; import com.st.systemsettings.contract.SystemContract; import com.st.systemsettings.manager.OtaManager; import com.st.systemsettings.manager.SystemManager; import com.st.systemsettings.manager.WifiNetManager; import com.st.systemsettings.utils.UserModeUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import static org.junit.Assert.*; import static org.mockito.Mockito.*; @RunWith(PowerMockRunner.class) @PrepareForTest({SystemManager.class, OtaManager.class, WifiNetManager.class, UserModeUtils.class}) public class SystemPresenterImplTest { private SystemPresenterImpl systemPresenter; @Mock private SystemContract.View mockView; @Mock private Context mockContext; @Before public void setUp() throws Exception { mockView = mock(SystemContract.View.class); mockContext = mock(Context.class); when(mockView.getContext()).thenReturn(mockContext); // 模拟单例对象 SystemManager mockSystemManager = mock(SystemManager.class); OtaManager mockOtaManager = mock(OtaManager.class); WifiNetManager mockWifiNetManager = mock(WifiNetManager.class); UserModeUtils mockUserModeUtils = mock(UserModeUtils.class); // mock 单例的 getInstance() 方法 PowerMockito.mockStatic(SystemManager.class); PowerMockito.when(SystemManager.getInstance()).thenReturn(mockSystemManager); PowerMockito.mockStatic(OtaManager.class); PowerMockito.when(OtaManager.getInstance()).thenReturn(mockOtaManager); PowerMockito.mockStatic(WifiNetManager.class); PowerMockito.when(WifiNetManager.getInstance()).thenReturn(mockWifiNetManager); PowerMockito.mockStatic(UserModeUtils.class); PowerMockito.when(UserModeUtils.getInstance()).thenReturn(mockUserModeUtils); when(mockUserModeUtils.isChildMode()).thenReturn(false); // 初始化被测试类 systemPresenter = new SystemPresenterImpl(); systemPresenter.registerView(mockView); systemPresenter.init(); } @Test public void testGetSystemList_UsingReflection() throws Exception { Method method = SystemPresenterImpl.class.getDeclaredMethod("getSystemList"); method.setAccessible(true); // 允许访问私有方法 List<SystemBean> systemList = (List<SystemBean>) method.invoke(systemPresenter); assertNotNull(systemList); assertEquals(6, systemList.size()); assertEquals(R.string.home_item_system, systemList.get(0).getLabel()); assertEquals(R.string.system_label_about_machine, systemList.get(1).getLabel()); assertEquals(R.string.system_label_online_update, systemList.get(2).getLabel()); assertEquals(R.string.system_label_local_upgrade, systemList.get(3).getLabel()); assertEquals(R.string.system_label_language_settings, systemList.get(4).getLabel()); assertEquals(R.string.system_label_reset_default, systemList.get(5).getLabel()); } @Test public void testInit_RegistersViewAndInitializesManagers() { verify(mockView, times(1)).getContext(); assertNotNull(systemPresenter); } } 上面分别是我的待测试的代码和单元测试代码,请继续帮我完成单元测试的代码
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值