文件名:SimpleCalculatorActivity.java
Xml文件名:simple_calculator.xml
我收到错误消息数据绑定Android - 类型参数T在行binding = DataBindingUtil.setContentView(this, R.layout.simple_calculator);
我尝试重命名xml文件并将其重命名为原始名称。但对我没用。
SimpleCalculatorActivity.java
package com.example.borntoflirt.design1;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.text.DecimalFormat;
public class SimpleCalculatorActivity extends AppCompatActivity {
private SimpleCalculatorActivity binding;
// This simple calculator can only perform operations of either addition, subtraction, multiplication or division.
// So we define four static chars to represent these operations
private static final char ADDITION = '+';
private static final char SUBTRACTION = '-';
private static final char MULTIPLICATION = '*';
private static final char DIVISION = '/';
// Set variable for operations
private double digit;
private double total = Double.NaN;
private String CURRENT_ACTION;
private DecimalFormat decimalFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
decimalFormat = new DecimalFormat("#.##########");
binding = DataBindingUtil.setContentView(this, R.layout.simple_calculator);
}
}
答案 0 :(得分:0)
绑定变量应为:
SimpleCalculatorBinding binding = DataBindingUtil.setContentView(this, R.layout.simple_calculator);
请关注this post
默认情况下,将根据布局文件的名称生成Binding类,将其转换为Pascal大小写和后缀" Binding"它。上面的布局文件是main_activity.xml,因此生成类是MainActivityBinding。
例如:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"/>
</LinearLayout>
</layout>