gpt4 book ai didi

java - 借助 AlertDialog 将新对象添加到自定义数组适配器

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:58 26 4
gpt4 key购买 nike

我正在尝试借助对话框添加新客户端,但我的编辑文本似乎不存在。

看起来所有 ID 都是正确的,并且 Activity 生命周期一切都很好,但我得到:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at dartdev.intent.MainActivity$3.onClick(MainActivity.java:122)

上线:

     String nameValue = clientName.getText().toString().trim();

谁能解释一下出了什么问题吗?

MainActivity.java

public class MainActivity extends Activity{

ArrayList<Client> clientListItems = new ArrayList<Client>();
ClientAdapter clientAdapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);

initClients();

clientAdapter = new ClientAdapter(this, clientListItems);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(clientAdapter);

Button buttonView = (Button)findViewById(R.id.addListItem);

buttonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

setAddingDialog();

}
});



}

/*
@Override
protected void onStart() {
super.onStart();
Button buttonView = (Button)findViewById(R.id.addListItem);

buttonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//listItems.add(0, Utils.hashGenerator(25));
//adapter.notifyDataSetChanged();

//DialogFragment newFragment = new AddingDialog();
//newFragment.show(getFragmentManager(), "adding");
setAddingDialog();

}
});
}
*/

void initClients(){
clientListItems.add(new Client("alex", 1265, new Wallet("151516456464564654", 4564.56), R.drawable.ic_launcher_foreground));
clientListItems.add(new Client("mike", 26, new Wallet("465456445644123231", 1645.2), R.drawable.ic_launcher_foreground));
clientListItems.add(new Client("tray", 145, new Wallet("12315465489789", 0.00), R.drawable.ic_launcher_foreground));
clientListItems.add(new Client("vincent", 999, new Wallet("3213546549789", 1000000.01), R.drawable.ic_launcher_foreground));
clientListItems.add(new Client("venom", 666, new Wallet("321154654654798", 145.6), R.drawable.ic_launcher_foreground));
}

void setAddingDialog(){

AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();

Dialog alertDialog = builder.create();

final EditText clientName = (EditText) alertDialog.findViewById(R.id.newClientName);
final EditText clientId = (EditText) alertDialog.findViewById(R.id.newClientId);
final EditText clientWallet = (EditText) alertDialog.findViewById(R.id.newClientWallet);
final EditText clientBalance = (EditText) alertDialog.findViewById(R.id.newClientBalance);

builder.setView(inflater.inflate(R.layout.dialog_add_new_client, null))
.setPositiveButton("add", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String nameValue = clientName.getText().toString().trim();
int idValue = Integer.valueOf(clientId.getText().toString().trim());
String walletValue = clientWallet.getText().toString().trim();
double balanceValue = Double.valueOf(clientBalance.getText().toString().trim());
clientListItems.add(new Client(nameValue, idValue, new Wallet(walletValue, balanceValue), R.drawable.ic_launcher_foreground));
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
})
.show();

}
}

dialog_add_new_client.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/newClientName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="name"
android:inputType="text" />

<EditText
android:id="@+id/newClientId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="id"
android:inputType="number" />

<EditText
android:id="@+id/newClientWallet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="walletNumber"
android:inputType="text" />

<EditText
android:id="@+id/newClientBalance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="balance"
android:inputType="numberDecimal" />

</LinearLayout>

最佳答案

移动此代码:

 Dialog alertDialog = builder.create();

final EditText clientName = (EditText) alertDialog.findViewById(R.id.newClientName);
final EditText clientId = (EditText) alertDialog.findViewById(R.id.newClientId);
final EditText clientWallet = (EditText) alertDialog.findViewById(R.id.newClientWallet);
final EditText clientBalance = (EditText) alertDialog.findViewById(R.id.newClientBalance);

之后

builder.setView(inflater.inflate(R.layout.dialog_add_new_client, null))
.setPositiveButton("add", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String nameValue = clientName.getText().toString().trim();
int idValue = Integer.valueOf(clientId.getText().toString().trim());
String walletValue = clientWallet.getText().toString().trim();
double balanceValue = Double.valueOf(clientBalance.getText().toString().trim());
clientListItems.add(new Client(nameValue, idValue, new Wallet(walletValue, balanceValue), R.drawable.ic_launcher_foreground));
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});

当您分配 EditText 时,对话框自定义 View 尚未展开,因此它们均为空。而且您还需要在展开的 View 上调用 findViewById,而不是在警报对话框上。

在设置自定义布局时不要调用 show(),但仅在最后,在 textviews 初始化之后,通过调用:

alertDialog.show();

编辑

将您的 EditText 声明为 Activity 的全局字段,因此它们不需要是最终的。

总结一下,这样改变你的setAddingDialog():

private EditText clientName;
private EditText clientId;
private EditText clientWallet;
private EditText clientBalance;

void setAddingDialog(){

AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();

View dialogView = inflater.inflate(R.layout.dialog_add_new_client, null);
builder.setView(dialogView)
.setPositiveButton("add", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String nameValue = clientName.getText().toString().trim();
int idValue = Integer.valueOf(clientId.getText().toString().trim());
String walletValue = clientWallet.getText().toString().trim();
double balanceValue = Double.valueOf(clientBalance.getText().toString().trim());
clientListItems.add(new Client(nameValue, idValue, new Wallet(walletValue, balanceValue), R.drawable.ic_launcher_foreground));
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});

Dialog alertDialog = builder.create();

clientName = (EditText) dialogView.findViewById(R.id.newClientName);
clientId = (EditText) dialogView.findViewById(R.id.newClientId);
clientWallet = (EditText) dialogView.findViewById(R.id.newClientWallet);
clientBalance = (EditText) dialogView.findViewById(R.id.newClientBalance);

alertDialog.show();
}

更新您需要在展开的 View 上而不是在警报对话框上调用 findViewById 。检查我更新的代码。

关于java - 借助 AlertDialog 将新对象添加到自定义数组适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48046354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com