android - 使用 ButterKnife 在 Dialog 中定义的 Button 的空对象引用
问题描述
我在通过 ButterKnife 在对话框屏幕中定义对象时遇到问题。我可以定义一个持有者类来确定在对话框屏幕中使用的每个对象。当我运行该应用程序时,它会引发如下所示的错误。
java.lang.NullPointerException: Attempt to read from field 'android.widget.Button com.example.dialog.AlertDialogActivity$ViewHolder.buttonUyeOl' on a null object reference
如何通过 ButterKnifer 将每个对象连接到对话屏幕?
我该如何解决?
这是我的代码如下所示。
public class AlertDialogActivity extends AppCompatActivity {
@BindView(R.id.dialogAc)
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
ButterKnife.bind(this);
openDialog();
}
private void openDialog() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialogScreen();
}
});
}
private void openDialogScreen() {
LayoutInflater ınflater = this.getLayoutInflater();
View view = ınflater.inflate(R.layout.alert_dialog, null);
final ViewHolder holder;
if (view != null) {
holder = (ViewHolder) view.getTag();
} else {
holder = new ViewHolder(view);
view.setTag(holder);
}
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(view);
alert.setCancelable(false);
final AlertDialog dialog = alert.create();
holder.buttonUyeOl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), holder.mailEditText.getText().toString() + " "
+ holder.kadiEditText.getText().toString() + " " + holder.sifreEditText.getText().toString()
, Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
holder.buttonCik.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
dialog.show();
}
static final class ViewHolder {
@BindView((R.id.mailAdres))
EditText mailEditText;
@BindView(R.id.kullanici)
EditText kadiEditText;
@BindView(R.id.sifre)
EditText sifreEditText;
@BindView(R.id.buttonUyeOl)
Button buttonUyeOl;
@BindView(R.id.buttonExit)
Button buttonCik;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
XML 文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#324589"
android:orientation="vertical"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/kullanici"
android:hint="Kullanıcı İsmini Giriniz"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mailAdres"
android:hint="Mail Adresi Giriniz"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sifre"
android:inputType="textPassword"
android:hint="Şifre Giriniz"/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Uye Ol"
android:id="@+id/buttonUyeOl"
/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="İptal Et"
android:id="@+id/buttonExit"
/>
</LinearLayout>
</LinearLayout>
解决方案
我的答案
private void openDialogScreen() {
LayoutInflater ınflater = this.getLayoutInflater();
View view = ınflater.inflate(R.layout.alert_dialog, null);
final ViewHolder holder = new ViewHolder(view);
....
}
推荐阅读
- git - 如何在循环工作流中的以前版本和当前工作之间共享修复?
- java - Eclipse Photon 过多的垃圾收集
- firebase - Firebase 的 scrypt 版本不提供与 save 相同的哈希值
- javascript - 如何将动态对象附加到 HTML 元素?
- redis - Redis 在行动 - 不公平的信号量
- windows - Firebase Unity 是否支持 Windows UWP 或 Windows 10 应用程序?
- firebase - Azure 和 FCM 之间的设备注册如何工作
- android - 从android中的视图中剪切一个形状
- reactjs - 在 React.js 中将对象添加到数组
- javascript - 两个 webpack 模块之间的重用函数