因此,我正在创建我的第一个功能性应用程序,即,我对android开发和编码总体上还是很陌生。因此,在我的应用程序中,每当我要插入新联系人(包含街道地址,电话号码和日期)时,都会弹出“警报”对话框。这是一些代码。
包com.profiapp.profi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
public class DataListFragment extends Fragment {
private RecyclerView contactList;
private ContactListAdapter adapter;
private LinearLayoutManager layoutManager;
private SQLiteDatabase mDatabase;
private static DataListFragment instance;
private AlertDialog optionDialog;
public DataListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
contactList = (RecyclerView) inflater.inflate(R.layout.fragment_data_list,
container, false);
layoutManager = new LinearLayoutManager(getActivity());
contactList.setLayoutManager(layoutManager);
ProfiDatabase profi = new ProfiDatabase(getContext());
mDatabase = profi.getWritableDatabase();
adapter = new ContactListAdapter(getContext(), getAllItems());
contactList.setAdapter(adapter);
adapter.setListener(new ContactListAdapter.Listener() {
@Override
public void onLongClick(long id, String nr, String ad, String dat) {
openOptionDialog(id, nr, ad, dat);
}
});
instance = this;
return contactList;
}
public void onStart(){
super.onStart();
}
public Cursor getAllItems() {
return mDatabase.query(
ContactContract.ContactEntry.TABLE_NAME,
null,
null,
null,
null,
null,
"_id DESC"
);
}
public static DataListFragment getInstance(){
return instance;
}
public void updateCursor(){
adapter.swapCursor(getAllItems());
}
public void openOptionDialog(final long entryId, final String nr, final String ad, final String dat){
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0){
NewContactDialogFragment fragment = new NewContactDialogFragment();
fragment.show(getActivity().getSupportFragmentManager(), "contact");
fragment.onEditContact(nr, ad, dat);
}else if(position == 1){
deleteEntry(entryId);
optionDialog.dismiss();
}
}
} ;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getLayoutInflater();
View optionMenu = inflater.inflate(R.layout.fragment_option_alert_dialog, null);
builder.setView(optionMenu);
builder.setTitle("Nustatymai");
ListView optionsList = optionMenu.findViewById(R.id.option_list);
optionsList.setOnItemClickListener(itemClickListener);
builder.setNeutralButton(R.string.atsaukti, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setCancelable(true);
optionDialog = builder.create();
optionDialog.show();
}
public void deleteEntry(long id){
mDatabase.delete(ContactContract.ContactEntry.TABLE_NAME,
"_id = ?",
new String[]{Long.toString(id)});
updateCursor();
}
}
package com.profiapp.profi;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import static com.profiapp.profi.ProfiDatabase.insertContact;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class NewContactDialogFragment extends DialogFragment {
private AlertDialog contact;
private static View dialogView2;
private View dialogView;
private EditText numerisMade;
private EditText addressMade;
private TextView dateMade;
public NewContactDialogFragment() {
// Required empty public constructor
}
public class onClick implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.input_date:
new DataPickerFragment().show(getActivity().getSupportFragmentManager(),
"datapicker");
}
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
dialogView = inflater.inflate(R.layout.fragment_new_contact_dialog, null);
dialogView2 = dialogView;
TextView date = dialogView.findViewById(R.id.input_date);
Date currentDate = Calendar.getInstance().getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String currentDateTimeString = format.format(currentDate);
date.setText(currentDateTimeString);
date.setOnClickListener(new onClick());
final EditText numeris = dialogView.findViewById(R.id.input_number);
final EditText address = dialogView.findViewById(R.id.input_address);
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(!numeris.getText().toString().isEmpty() && !address.getText().toString().isEmpty()
&& numeris.getText().toString().trim().length() == 11 || numeris.getText().toString().trim().length() == 9) {
contact.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}else{
contact.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
}
};
numeris.addTextChangedListener(textWatcher);
address.addTextChangedListener(textWatcher);
builder.setView(dialogView)
.setTitle("Prideti nauja adresata")
.setPositiveButton(R.string.prideti, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TextView date = dialogView.findViewById(R.id.input_date);
long nr = Long.parseLong(numeris.getText().toString());
String adresas = address.getText().toString();
String data = date.getText().toString();
onPrideti(nr, adresas, data);
}
})
.setNegativeButton(R.string.atsaukti, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
contact = builder.create();
contact.setCancelable(true);
return contact;
}
public void onStart(){
super.onStart();
contact.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
public void onPrideti(long nr, String adresas, String data){
SQLiteOpenHelper profi = new ProfiDatabase(getContext());
SQLiteDatabase db = profi.getWritableDatabase();
insertContact(db, adresas, nr, data);
db.close();
DataListFragment.getInstance().updateCursor();
}
public static void setNewDate(String date){
TextView dateInput = dialogView2.findViewById(R.id.input_date);
dateInput.setText(date);
}
public void onEditContact(String nr, String adresas, String data){
numerisMade = dialogView.findViewById(R.id.input_number);
addressMade = dialogView.findViewById(R.id.input_address);
dateMade = dialogView.findViewById(R.id.input_date);
numerisMade.setText(nr);
addressMade.setText(adresas);
dateMade.setText(data);
}
}
现在,我要使用与添加新联系人相同的AlertDialog来编辑联系人的详细信息。到目前为止,我已经成功地从数据库中检索了必要的数据,但是,每当我尝试向AlertDialog中添加文本时,都会出现此异常“试图在空对象引用上调用虚拟方法”。我有点想当我调用setter方法(onEditContact())来设置我的AlertDialog的值时,视图尚未初始化,因此是异常,但我想知道有什么办法可以解决这个问题并填充与AlertDialog一起创建后的视图?
答案 0 :(得分:0)
我通过创建相同的AlerDialog片段并将我的值传递给其onCreate方法,而不是在创建数据后尝试传递数据来解决了该问题。不知道是否是正确的方法,但是它像一个魅力一样