我正在处理要通过单击“对话框”按钮打开“活动”的应用程序。层次结构是单击List_data时,应打开一个对话框,然后单击对话框按钮。它将打开一个新活动。
当我单击“对话框”中的“按钮”时,对话框正在完美地打开按钮,它使应用程序崩溃并出现空指针异常。
这是我的List_Activity,数据显示在列表中,工作正常。
public class UsersListActivity extends AppCompatActivity implements ItemClickListenerLongPressed{
AppCompatActivity activity = UsersListActivity.this;
AppCompatTextView textViewName;
RecyclerView mRecyclerView;
AppCompatButton textViewButtonNewUser;
UserRecyclerAdapterSavedUsers userRecyclerAdapterSavedUsers;
List<User> listUsers;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_record_updated_list);
mRecyclerView= (RecyclerView) findViewById(R.id.recyclerViewUsers);
mRecyclerView.setAdapter(userRecyclerAdapterSavedUsers);
//userRecyclerAdapterSavedUsers.setItemClickListenerLongPressed(this);
initViews();
initObjects();
}
@Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(UsersListActivity.this,AdminMain.class));
finish();
}
@Override
protected void onRestart() {
super.onRestart();
}
/**
* This method is to initialize views
*/
private void initViews() {
textViewName = (AppCompatTextView) findViewById(R.id.textViewName);
textViewButtonNewUser = (AppCompatButton) findViewById(R.id.btnaddnew);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerViewUsers);
textViewButtonNewUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(UsersListActivity.this,UserRecordSaveActivity.class));
}
});
}
/**
* This method is to initialize objects to be used
*/
private void initObjects() {
listUsers = new ArrayList<>();
userRecyclerAdapterSavedUsers = new UserRecyclerAdapterSavedUsers(listUsers);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(userRecyclerAdapterSavedUsers);
databaseHelper = new DatabaseHelper(activity);
String emailFromIntent = getIntent().getStringExtra("USERS");
textViewName.setText(emailFromIntent);
getDataFromSQLite();
}
/**
* This method is to fetch all user records from SQLite
*/
private void getDataFromSQLite() {
// AsyncTask is used that SQLite operation not blocks the UI Thread.
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
listUsers.clear();
listUsers.addAll(databaseHelper.getAllUser());
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
userRecyclerAdapterSavedUsers.notifyDataSetChanged();
}
}.execute();
}
@Override
public void onClick(View view, int position) {
}
}
这是我在其中实现对话框的适配器类
public class UserRecyclerAdapterSavedUsers extends RecyclerView.Adapter<UserRecyclerAdapterSavedUsers.UserViewHolder> {
private List<User> listUsers;
Context mContext;
ItemClickListenerLongPressed itemClickListenerLongPressed;
View itemView;
public UserRecyclerAdapterSavedUsers(List<User> listUsers) {
this.listUsers = listUsers;
}
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_user_recycler_second, parent, false);
return new UserViewHolder(itemView);
}
@Override
public void onBindViewHolder(UserViewHolder holder, int position) {
holder.textViewID.setText(listUsers.get(position).getUserid());
holder.textViewName.setText(listUsers.get(position).getName());
holder.textViewPassword.setText(listUsers.get(position).getPassword());
holder.textViewRole.setText(listUsers.get(position).getRole());
}
public void setItemClickListenerLongPressed(ItemClickListenerLongPressed itemClickListenerLongPressed) {
this.itemClickListenerLongPressed = itemClickListenerLongPressed;
}
@Override
public int getItemCount() {
Log.v(UsersRecyclerAdapter.class.getSimpleName(),""+listUsers.size());
return listUsers.size();
}
private void displayingAlertDialog() {
final User user= new User();
//displaying alert dialog box
AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
builder.setTitle("Choose Option");
builder.setMessage("Update or Delete?");
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//go to update activity
gotoupdateuserActivity(user.getUserid());
// dialog.cancel();
}
});
builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//go to update activity
dialog.cancel();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert11 = builder.create();
alert11.show();
}
private void gotoupdateuserActivity(String userid) {
Intent goToUpdate = new Intent(mContext, SplashActivity.class);
goToUpdate.putExtra("USER_ID", userid);
mContext.startActivity(goToUpdate);
}
/**
* ViewHolder class
*/
public class UserViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//public AppCompatTextView ID;
public AppCompatTextView textViewID;
public AppCompatTextView textViewName;
public AppCompatTextView textViewPassword;
public AppCompatTextView textViewRole;
LinearLayout layout;
public UserViewHolder(View view) {
super(view);
textViewID = (AppCompatTextView) view.findViewById(R.id.textViewID);
textViewName = (AppCompatTextView) view.findViewById(R.id.textViewName);
textViewPassword = (AppCompatTextView) view.findViewById(R.id.textViewPassword);
textViewRole = (AppCompatTextView) view.findViewById(R.id.textViewRole);
layout = (LinearLayout) view.findViewById(R.id.list_view);
layout.setOnClickListener(this);
}
@Override
public void onClick(View v) {
displayingAlertDialog();
}
}
}
这是错误图片
答案 0 :(得分:1)
您似乎没有初始化mContext
。您可以在UserRecyclerAdapterSavedUsers
中进行以下操作:
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Add this line to assign the context.
mContext = parent.getContext();
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_user_recycler_second, parent, false);
return new UserViewHolder(itemView);
}
答案 1 :(得分:1)
您正在传递上下文而未对其进行初始化
Context mContext;
public UserRecyclerAdapterSavedUsers(List<User> listUsers) {
this.listUsers = listUsers;
}
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_user_recycler_second, parent, false);
mContext = parent.getContext() //INITIALISE CONTEXT
return new UserViewHolder(itemView);
}