我正在项目中实现两种方式的数据绑定,但是无法在导航抽屉活动中进行数据绑定。我试过了,但它给出了一个错误。请提供相同的示例代码。
content_policy_info.xml
type="com.exlservice.lifeprov1.service.model.GetPolicyInfoResponse" />
type="com.exlservice.lifeprov1.service.model.SurrenderQuoteResponse" />
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:id="@+id/c1"
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:cOntext=".view.ui.PolicyInfo"
tools:showIn="@layout/app_bar_policy_info">
android:background="#223d50"
android:layout_>
android:layout_
android:orientation="vertical">
android:orientation="vertical"
android:background="@drawable/policy_prime_info_bg"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
android:layout_
android:layout_marginTop="8dp"
android:orientation="horizontal">
android:layout_marginLeft="5dp"
android:layout_
android:text="@{policyInfo.ProductId}"
android:textColor="#76c6db"
android:layout_weight="1"
android:typeface="serif"
android:text
android:id="@+id/id_fc_first_key"/>
android:textColor="#76c6db"
android:text
android:typeface="serif"
android:layout_
android:text="@{policyInfo.Contract}"
android:layout_weight="1"
android:id="@+id/id_fc_second_key"/>
PolicyInfo.java
public class PolicyInfo extends AppCompatactivity
implements NavigationView.OnNavigationItemSelectedListener {
APIInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding= DataBindingUtil.setContentView(this,R.layout.activity_policy_info);
Intent intent = getIntent();
String sCompanyCode = intent.getStringExtra("CompanyCode");
String sPolicyNumber = intent.getStringExtra("PolicyNumber");
String sWayPoint = intent.getStringExtra("WayPoint");
Log.e("onCreate: ",sCompanyCode+" "+sPolicyNumber );
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String currentTime = sdf.format(new Date());
SurrenderQuoteInput surrenderQuoteInput = new SurrenderQuoteInput();
surrenderQuoteInput.setCoderID("TRN7");
surrenderQuoteInput.setCompanyCode("01");
PolicyDetailsReq policyDetailsReq= new PolicyDetailsReq();
policyDetailsReq.setCoderID("TRN7");
getPolicyInfoRequest.setPolicyDetailsReq(policyDetailsReq);
PolicyInfoViewModel.Factory factory = new PolicyInfoViewModel.Factory(
getapplication(),getPolicyInfoRequest,surrenderQuoteRequest);
pd = new ProgressDialog(PolicyInfo.this);
//setContentView(R.layout.activity_policy_info);
pref= getSharedPreferences("LoginDetail",0);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportactionBar(toolbar);
getSupportactionBar().setTitle("Poilcy Info");
apiInterface = APIClient.getclient().create(APIInterface.class);
FloatingactionButton fab = findViewById(R.id.fab);
fab.setOnClicklistener(new View.OnClicklistener() {
@Override
public void onClick(View view) {
snackbar.make(view,"Replace with your own action",snackbar.LENGTH_LONG)
.setaction("action",null).show();
}
});
apiInterface = APIClient.getclient().create(APIInterface.class);
PolicyInfoViewModel viewModel =
ViewModelProviders.of(this,factory).get(PolicyInfoViewModel.class);
**observeViewModel**(viewModel);
pd.setTitle("Loading...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
// show it
pd.show();
DrawerLayout drawer = findViewById(R.id.drawer_layout);
actionbardrawertoggle toggle = new actionbardrawertoggle(
this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigatiOnView= findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void **observeViewModel**(final PolicyInfoViewModel viewModel) {
// Update the list when the data changes
viewModel.getPolicyInfoResponsetObservable().observe(this,new Observer
@Override
public void onChanged(@Nullable GetPolicyInfoResponse getPolicyInfoResponse) {
if(getPolicyInfoResponse!=null) {
viewModel.setPolicyInfoResult(getPolicyInfoResponse);
binding.setPolicyinfo(getPolicyInfoResponse);
}
/* if (getLastaccessedDetailsResult != null) {
getLastaccessedDetailsRes=getLastaccessedDetailsResult;
viewModel.setLastaccessedDetailsResult(getLastaccessedDetailsResult);
setupViewPager(viewPager);
}*/
}
});
}
以上错误显示类强制转换异常
android.support.v7.widget.AppCompatTextView无法转换为android.support.design.widget.NavigationView
完整堆栈跟踪
2019-11-05 16:28:27.673 3019-3019 / com.exlservice.lifeprov1 E / Android运行时:致命异常:主要
流程:com.exlservice.lifeprov1,PID:3019
java.lang.RuntimeException:无法启动活动ComponentInfo {com.exlservice.lifeprov1 / com.exlservice.lifeprov1.view.ui.PolicyInfo}:java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView无法转换为android.support.design.widget.NavigationView
在android.app.activityThread.performLaunchactivity(activityThread.java:2817)
在android.app.activityThread.handleLaunchactivity(activityThread.java:2892)
在android.app.activityThread.-wrap11(未知来源:0)
在android.app.activityThread $ H.handleMessage(activityThread.java:1593)
在android.os.Handler.dispatchMessage(Handler.java:105)
在android.os.Looper.loop(Looper.java:164)
在android.app.activityThread.main(activityThread.java:6541)
在java.lang.reflect.Method.invoke(本机方法)
在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
引起原因:java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView无法转换为android.support.design.widget.NavigationView
在com.exlservice.lifeprov1.databinding.activityPolicyInfoBindingImpl。(activityPolicyInfoBindingImpl.java:30)
在com.exlservice.lifeprov1.databinding.activityPolicyInfoBindingImpl。(activityPolicyInfoBindingImpl.java:27)
在com.exlservice.lifeprov1.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.java:56)
在android.databinding.MergedDataBinderMapper.getDataBinder(MergedDataBinderMapper.java:74)
在android.databinding.DataBindingUtil.bind(DataBindingUtil.java:199)
在android.databinding.DataBindingUtil.bindToAddedViews(DataBindingUtil.java:327)
在android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:306)
在android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
在com.exlservice.lifeprov1.view.ui.PolicyInfo.onCreate(PolicyInfo.java:58)
在android.app.activity.performCreate(activity.java:6975)
在android.app.Instrumentation.callactivityOnCreate(Instrumentation.java:1213)
在android.app.activityThread.performLaunchactivity(activityThread.java:2770)
在android.app.activityThread.handleLaunchactivity(activityThread.java:2892)
在android.app.activityThread.-wrap11(未知来源:0)
在android.app.activityThread $ H.handleMessage(activityThread.java:1593)
在android.os.Handler.dispatchMessage(Handler.java:105)
在android.os.Looper.loop(Looper.java:164)
在android.app.activityThread.main(activityThread.java:6541)
在java.lang.reflect.Method.invoke(本机方法)
在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
似乎您尝试使用AppCompatTextView初始化NavigationView
NavigationView navigatiOnView= findViewById(R.id.nav_view);
在onCreate()方法内的PolicyInfo活动行58中
尝试检查您在哪里使用此ID(R.id.nav_view)并正确初始化