대화 상자와 함께 새로운 클라이언트를 추가하려고하지만 편집 텍스트가 존재하지 않는 것 같습니다. 모든 ID를 맞아 모든 활동 라이브주기에 좋은처럼AlertDialog를 사용하여 사용자 정의 배열 어댑터에 새 객체 추가
같습니다,하지만 난 얻을 : 라인에
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>
감사합니다.하지만 도움이되지 않습니다. 이전 위치에서 이전 오류가 발생했습니다. –
네 말이 맞아, 미안해. 팽창 된 뷰에서는 alert 대화 상자가 아닌'findViewById'를 호출해야합니다. 내가 업데이트 한 코드를 확인하십시오. –