这是我的UserAdapter类
public class UserAdapter extends RecyclerView.Adapter<UserCardHolder> {
private Activity adapterActivity;
private LayoutInflater layoutInflater;
protected List<UserCardData> userCardDataList = new ArrayList<>();
public UserAdapter(Activity activity,List<UserCardData> userCardDataList){
this.adapterActivity= activity;
this.userCardDataList = userCardDataList;
layoutInflater = (LayoutInflater) adapterActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getItemCount() {
return userCardDataList.size();
}
public UserCardData removeItem(int position) {
final UserCardData model = userCardDataList.remove(position);
notifyItemInserted(position);
return model;
}
public void addItem(int position, UserCardData model) {
userCardDataList.add(position, model);
notifyItemInserted(position);
}
public void moveItem(int fromPosition, int toPosition) {
final UserCardData model = userCardDataList.remove(fromPosition);
userCardDataList.add(toPosition, model);
notifyItemMoved(fromPosition, toPosition);
}
public void animateTo(List<UserCardData> models) {
applyAndAnimateRemovals(models);
applyAndAnimateAdditions(models);
applyAndAnimateMovedItems(models);
}
private void applyAndAnimateRemovals(List<UserCardData> newModels) {
for (int i = userCardDataList.size() - 1; i >= 0; i--) {
final UserCardData model = userCardDataList.get(i);
if (!newModels.contains(model)) {
removeItem(i);
}
}
}
private void applyAndAnimateAdditions(List<UserCardData> newModels) {
for (int i = 0, count = newModels.size(); i < count; i++) {
final UserCardData model = newModels.get(i);
if (!userCardDataList.contains(model)) {
addItem(i, model);
}
}
}
private void applyAndAnimateMovedItems(List<UserCardData> newModels) {
for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
final UserCardData model = newModels.get(toPosition);
final int fromPosition = userCardDataList.indexOf(model);
if (fromPosition >= 0 && fromPosition != toPosition) {
moveItem(fromPosition, toPosition);
}
}
}
@Override
public UserCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_card_services, parent, false);
final UserCardHolder userCardHolder = new UserCardHolder(view);
userCardHolder.itemView.setTag(userCardHolder);
return userCardHolder;
}
@Override
public void onBindViewHolder(UserCardHolder userCardHolder, int position) {
UserCardData userCardData = userCardDataList.get(position);
Log.e("hello", "in bind view holder");
Log.e(" hello", userCardData.uname);
userCardHolder.txtuname.setText(userCardData.uname);
userCardHolder.txtphoneno.setText(userCardData.phoneno);
}
public long getItemId(int position) {
return 0;
}
}
这是我的UserCardView类
public class UserCardView extends Fragment {
private RecyclerView recyclerView;
private UserAdapter userAdapter;
private RecyclerView.LayoutManager layoutManager;
private DatabaseHelper databaseHelper;
private List<UserCardData> userCardDataList = new ArrayList<>();
public View onCreateView(LayoutInflater layoutInflater,ViewGroup viewGroup,Bundle onsavedinstance){
View view = layoutInflater.inflate(R.layout.recyclerview,viewGroup,false);
databaseHelper = DatabaseHelper.getDbInstance(getActivity().getApplicationContext());
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
userCardDataList = databaseHelper.getUserData();
userAdapter = new UserAdapter(getActivity(),userCardDataList) ;
recyclerView.setAdapter(userAdapter);
//userAdapter.animateTo(userCardDataList);
return view;
}
}
这是CardHolder类
public class UserCardHolder extends RecyclerView.ViewHolder {
public TextView txtuname,txtphoneno;
public UserCardHolder(View itemView){
super(itemView);
txtuname = (TextView) itemView.findViewById(R.id.text_username);
txtphoneno = (TextView) itemView.findViewById(R.id.text_phoneno);
}
}
当我运行应用程序时,我遇到了这个例外。
FATAL EXCEPTION: main
Process: com.example.mithilamin.servicestation1, PID: 7619
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.mithilamin.servicestation1.usercard.UserAdapter.onBindViewHolder(UserAdapter.java:93)
at com.example.mithilamin.servicestation1.usercard.UserAdapter.onBindViewHolder(UserAdapter.java:14)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15614)
at android.view.ViewGroup.layout(ViewGroup.java:4968)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2102)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1859)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5884)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Ha
答案 0 :(得分:3)
您的问题清楚地告诉 NullPointerException :
编辑2:
userCardHolder.txtuname is null.
请你替换下面的代码:
((UserCardHolder) holder).txtuname.setText(userCardData.uname);
((UserCardHolder) holder).txtphoneno.setText(userCardData.phoneno);
确保TextView没有null变量,因为您使用此变量来设置文本。
希望这是有道理的。
答案 1 :(得分:0)
你的错误非常明确,错误就是这一行
Insert into user_account values ('ABC', 0);
Insert into user_account values ('ABC', '0');
Insert into user_account values ('ABC', False);
Insert into user_account values ('ABC', 'False');
检查userCardHolder.txtuname.setText(userCardData.uname);
是否有layout_card_services.xml
名为TextView