我正在尝试使用数据绑定设置ProgressBar的进度。
这是我的布局:
<data>
<import type="java.lang.Integer"/>
<variable
name="sectionsViewModel"
type="com.techiespace.projects.hark.SectionsViewModel" />
</data>
<!--Other views-->
<android.support.v7.widget.CardView
android:id="@+id/bankcardId1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:onClick="onClickSection1"
app:cardBackgroundColor="@color/primary_light">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="1"
android:textSize="144sp" />
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress='@{Integer.parseInt(sectionsViewModel.getAccuracyLiveData[0].getAccuracy().split(" ")[1])}'
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='@{sectionsViewModel.getAccuracyLiveData[0].getAccuracy().split(" ")[1]}' />
</android.support.v7.widget.CardView>
<!--Other views-->
对sectionsViewModel.getAccuracyLiveData[0].getAccuracy().split(" ")[1]
的调用返回一个字符串准确度分数,随着将字符串设置为进度会产生以下错误,我已将其更改为整数:
Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'android:progress' with parameter type java.lang.String on android.widget.ProgressBar.
file:C:\Users\Shubham Sahu\StudioProjects\Hark\app\src\main\res\layout\activity_sections.xml
loc:64:44 - 64:111
****\ data binding error ****
第二个TextView中的android:text="..."
属性值已解析并正确显示(我通过使用自定义ProgressBar并将setProgress()调用放在try catch块中进行了验证)。但是在解决android:progress="..."
09-29 08:35:40.683 26771-26771/com.techiespace.projects.hark E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.techiespace.projects.hark, PID: 26771
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.techiespace.projects.hark/com.techiespace.projects.hark.SectionsActivity}: java.lang.RuntimeException: Failed to call observer method
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2584)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.RuntimeException: Failed to call observer method
at android.arch.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:225)
at android.arch.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:193)
at android.arch.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:184)
at android.arch.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:36)
at android.arch.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
at android.arch.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:292)
at android.arch.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:332)
at android.arch.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:137)
at android.arch.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:123)
at android.arch.lifecycle.ReportFragment.dispatch(ReportFragment.java:121)
at android.arch.lifecycle.ReportFragment.onStart(ReportFragment.java:81)
at android.app.Fragment.performStart(Fragment.java:2260)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1007)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1153)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1135)
at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1985)
at android.app.FragmentController.dispatchStart(FragmentController.java:163)
at android.app.Activity.performStart(Activity.java:6627)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2541)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.NumberFormatException: Invalid int: "null"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
at com.techiespace.projects.hark.databinding.ActivitySectionsBindingImpl.executeBindings(ActivitySectionsBindingImpl.java:224)
at android.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:437)
at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:409)
at android.databinding.ViewDataBinding$OnStartListener.onStart(ViewDataBinding.java:1621)
at java.lang.reflect.Method.invoke(Native Method)
at android.arch.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:215)
at android.arch.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:193)
at android.arch.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:184)
at android.arch.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:36)
at android.arch.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
at android.arch.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:292)
at android.arch.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:332)
at android.arch.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:137)
at android.arch.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:123)
at android.arch.lifecycle.ReportFragment.dispatch(ReportFragment.java:121)
at android.arch.lifecycle.ReportFragment.onStart(ReportFragment.java:81)
at android.app.Fragment.performStart(Fragment.java:2260)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1007)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1153)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1135)
at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1985)
at android.app.FragmentController.dispatchStart(FragmentController.java:163)
at android.app.Activity.performStart(Activity.java:6627)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2541)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
当设置进度时,TextView的text属性正确显示非null值时,我不明白为什么该值为null。
编辑1: SectionsViewModel.java
public class SectionsViewModel extends AndroidViewModel {
private LiveData<List<Clips>> accuracyLiveData;
public SectionsViewModel(@NonNull Application application, String vidId) {
super(application);
ClipsDao clipsDao = ClipDatabase.getDatabase(application).clipsDao();
accuracyLiveData = clipsDao.findLiveAccById(vidId);
}
public LiveData<List<Clips>> getAccuracyLiveData() {
return accuracyLiveData;
}
}
编辑2:活动
public class SectionsActivity extends AppCompatActivity {
String stopPoints;
String videoId;
String[] stopPointsArr;
Intent intent;
String originalXMLTranscript = "";
ActivitySectionsBinding sectionsActivityBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sections);
videoId = getIntent().getStringExtra("id");
stopPoints = getIntent().getStringExtra("stop_points");
originalXMLTranscript = getIntent().getStringExtra("xml_transcript");
stopPointsArr = stopPoints.split(" ");
intent = new Intent(this, EvaluateClipActivity.class);
intent.putExtra("id", videoId);
intent.putExtra("xml_transcript", originalXMLTranscript);
SectionsViewModel sectionsViewModel = ViewModelProviders.of(this, new SectionsViewModelFactory(this.getApplication(), videoId)).get(SectionsViewModel.class);
sectionsActivityBinding = DataBindingUtil.setContentView(this, R.layout.activity_sections);
sectionsActivityBinding.setLifecycleOwner(this);
sectionsActivityBinding.setSectionsViewModel(sectionsViewModel);
setContentView(sectionsActivityBinding.getRoot());
}
}
答案 0 :(得分:0)
我通过在ActivitySectionsBindingImpl.java中添加断点来调试代码
由于某种原因,该文件中的代码被两次调用。
第一次调用文件中的sectionsViewModelGetAccuracyLiveData.getValue()
函数时返回null,因此不会执行后续代码。
最后,有一个parseInt调用,而不检查传入的值是否有效。这是发生错误的地方。
在TextView中访问相同数据时,应用程序没有崩溃的原因是,除了Integer.parseInt()函数之外,所有函数都被检查为非空。
这里是修复之前生成的ActivitySectionsBindingImpl.java文件的一部分。
if (sectionsViewModel != null) {
// read sectionsViewModel.getAccuracyLiveData
sectionsViewModelGetAccuracyLiveData = sectionsViewModel.getAccuracyLiveData();
}
updateLiveDataRegistration(0, sectionsViewModelGetAccuracyLiveData);
if (sectionsViewModelGetAccuracyLiveData != null) {
// read sectionsViewModel.getAccuracyLiveData.getValue()
sectionsViewModelGetAccuracyLiveDataGetValue = sectionsViewModelGetAccuracyLiveData.getValue();
}
if (sectionsViewModelGetAccuracyLiveDataGetValue != null) {
// read sectionsViewModel.getAccuracyLiveData.getValue()[0]
sectionsViewModelGetAccuracyLiveData0 = getFromList(sectionsViewModelGetAccuracyLiveDataGetValue, 0);
}
if (sectionsViewModelGetAccuracyLiveData0 != null) {
// read sectionsViewModel.getAccuracyLiveData.getValue()[0].getAccuracy()
sectionsViewModelGetAccuracyLiveData0GetAccuracy = sectionsViewModelGetAccuracyLiveData0.getAccuracy();
}
if (sectionsViewModelGetAccuracyLiveData0GetAccuracy != null) {
// read sectionsViewModel.getAccuracyLiveData.getValue()[0].getAccuracy().split(" ")
sectionsViewModelGetAccuracyLiveData0GetAccuracySplitJavaLangString = sectionsViewModelGetAccuracyLiveData0GetAccuracy.split(" ");
}
if (sectionsViewModelGetAccuracyLiveData0GetAccuracySplitJavaLangString != null) {
// read sectionsViewModel.getAccuracyLiveData.getValue()[0].getAccuracy().split(" ")[6]
sectionsViewModelGetAccuracyLiveData0GetAccuracySplit6 = getFromArray(sectionsViewModelGetAccuracyLiveData0GetAccuracySplitJavaLangString, 6);
// read sectionsViewModel.getAccuracyLiveData.getValue()[0].getAccuracy().split(" ")[1]
sectionsViewModelGetAccuracyLiveData0GetAccuracySplit1 = getFromArray(sectionsViewModelGetAccuracyLiveData0GetAccuracySplitJavaLangString, 1);
// read sectionsViewModel.getAccuracyLiveData.getValue()[0].getAccuracy().split(" ")[4]
sectionsViewModelGetAccuracyLiveData0GetAccuracySplit4 = getFromArray(sectionsViewModelGetAccuracyLiveData0GetAccuracySplitJavaLangString, 4);
// read sectionsViewModel.getAccuracyLiveData.getValue()[0].getAccuracy().split(" ")[7]
sectionsViewModelGetAccuracyLiveData0GetAccuracySplit7 = getFromArray(sectionsViewModelGetAccuracyLiveData0GetAccuracySplitJavaLangString, 7);
// read sectionsViewModel.getAccuracyLiveData.getValue()[0].getAccuracy().split(" ")[2]
sectionsViewModelGetAccuracyLiveData0GetAccuracySplit2 = getFromArray(sectionsViewModelGetAccuracyLiveData0GetAccuracySplitJavaLangString, 2);
// read sectionsViewModel.getAccuracyLiveData.getValue()[0].getAccuracy().split(" ")[5]
sectionsViewModelGetAccuracyLiveData0GetAccuracySplit5 = getFromArray(sectionsViewModelGetAccuracyLiveData0GetAccuracySplitJavaLangString, 5);
// read sectionsViewModel.getAccuracyLiveData.getValue()[0].getAccuracy().split(" ")[3]
sectionsViewModelGetAccuracyLiveData0GetAccuracySplit3 = getFromArray(sectionsViewModelGetAccuracyLiveData0GetAccuracySplitJavaLangString, 3);
}
// read Integer.parseInt(sectionsViewModel.getAccuracyLiveData.getValue()[0].getAccuracy().split(" ")[1])
integerParseIntSectionsViewModelGetAccuracyLiveData0GetAccuracySplit1 = java.lang.Integer.parseInt(sectionsViewModelGetAccuracyLiveData0GetAccuracySplit1);
修复 在我的xml文件中,简单地添加了一个空检查
android:progress= '@{sectionsViewModel.getAccuracyLiveData[0].getAccuracy().split(" ")[1] != null ? (int)Double.parseDouble(sectionsViewModel.getAccuracyLiveData[0].getAccuracy().split(" ")[1]) : 50}'
尽管这解决了问题,但我不完全理解为什么ActivitySectionsBindingImpl.java被两次调用。