Nikhyl Nikhyl - 1 month ago 30
Android Question

Android-Firebase api

My android application is giving an error on Null pointer Exception on Attempting an invoke to virtual method String.equals() on null object reference but not giving any line number on which the error occurred in code, what is the problem please give me solution

here is my logCat window error output

7.112 12507-12507/com.nikhil.mybvb2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nikhil.mybvb2, PID: 12507
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:715)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:167)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5836)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5060)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:553)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2843)
at android.view.View.measure(View.java:17810)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17810)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17810)
at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1411)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:459)
at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:479)
at android.view.View.measure(View.java:17810)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:700)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1364)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:765)
at android.view.View.measure(View.java:17810)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:459)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17810)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17810)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:459)
at android.view.View.measure(View.java:17810)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5625)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17


public class TimelineSingleActivity extends AppCompatActivity {

public static class CommentViewHolder extends RecyclerView.ViewHolder {

View mView;
TextView commentText;
TextView commenterName;
CircularImageView commenterImage;


public CommentViewHolder(View itemView) {
super(itemView);
mView = itemView;
commentText = (TextView) itemView.findViewById(R.id.comment_text);
commenterName = (TextView) itemView.findViewById(R.id.commenter_name);
commenterImage = (CircularImageView) itemView.findViewById(R.id.commenterImage);
}
}


private String mPost_key = null;
//single image details
private TextView userName;
private TextView postSingleDesc;
private ImageView postSingleImage;
private FloatingActionButton fabDelet;
private NestedScrollView nestedView;

//comment Details
private String mCurrentUser;
private String mPhotoUrl;
private String userid;
private RecyclerView commentingRecyclerView;
private ImageButton sendButton;
private EditText commentText;
private FirebaseRecyclerAdapter<Comments, CommentViewHolder> mFirebaseAdapter;


private DatabaseReference mCommentingDatabase;
private DatabaseReference mDataBase;
private DatabaseReference mDatabaseUsers;
private FirebaseAuth mAuth;
private FirebaseUser firebaseUser;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timeline_single);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

mPost_key = getIntent().getExtras().getString("post_id");
mAuth = FirebaseAuth.getInstance();
firebaseUser = mAuth.getCurrentUser();



final CollapsingToolbarLayout collapse = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);

userName = (TextView) findViewById(R.id.user_onpost_name);
postSingleDesc = (TextView) findViewById(R.id.single_post_desc);
postSingleImage = (ImageView) findViewById(R.id.single_post_image);
fabDelet = (FloatingActionButton) findViewById(R.id.delete_post_btn);


mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("users");
mDatabaseUsers.keepSynced(true);
mDataBase = FirebaseDatabase.getInstance().getReference().child("timeline_pic_uploads");
mDataBase.keepSynced(true);
// userid =firebaseUser.getUid();
mDataBase.child(mPost_key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

String post_title = (String) dataSnapshot.child("title").getValue();
String post_desc = (String) dataSnapshot.child("desc").getValue();
final String post_image = (String) dataSnapshot.child("image").getValue();
String post_uid = (String) dataSnapshot.child("uid").getValue();
String post_username = (String) dataSnapshot.child("username").getValue();

userName.setText("-Posted by " + post_username);
collapse.setTitle(post_title);
postSingleDesc.setText(post_desc);
Picasso.with(TimelineSingleActivity.this).load(post_image).networkPolicy(NetworkPolicy.OFFLINE).into(postSingleImage, new Callback() {
@Override
public void onSuccess() {
}

@Override
public void onError() {

Picasso.with(TimelineSingleActivity.this).load(post_image).into(postSingleImage);

}
});

/*if (userid!=null && userid.equals(post_uid)) {
Toast.makeText(TimelineSingleActivity.this, "Errorrrrrrr", Toast.LENGTH_SHORT).show();
fabDelet.setVisibility(View.VISIBLE);
}*/

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


fabDelet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

mDataBase.child(mPost_key).removeValue();

Intent main = new Intent(TimelineSingleActivity.this, MainActivity.class);
main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(main);
}
});


//click to image View to open new fullscreen activity
postSingleImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent fullImage = new Intent(TimelineSingleActivity.this, FullImageActivity.class);
fullImage.putExtra("image_id", mPost_key);
startActivity(fullImage);
}
});


//commenting section
commentingRecyclerView = (RecyclerView) findViewById(R.id.commenting_view);
final LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setStackFromEnd(true);

mCommentingDatabase = FirebaseDatabase.getInstance().getReference().child("comments");
mCommentingDatabase.keepSynced(true);
mFirebaseAdapter = new FirebaseRecyclerAdapter<Comments, CommentViewHolder>(
Comments.class,
R.layout.single_comment,
CommentViewHolder.class,
mCommentingDatabase) {
@Override
protected void populateViewHolder(CommentViewHolder viewHolder, Comments model, final int position) {
viewHolder.commentText.setText(model.getText());
viewHolder.commenterName.setText(model.getName());
if (model.getPhotoUrl() == null) {
viewHolder.commenterImage.setImageDrawable(ContextCompat.getDrawable(TimelineSingleActivity.this,
R.drawable.user_pic));
} else {
Picasso.with(TimelineSingleActivity.this).load(model.getPhotoUrl()).into(viewHolder.commenterImage);
}

viewHolder.mView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(TimelineSingleActivity.this, " " + position, Toast.LENGTH_LONG).show();
return false;
}
});
}

};


commentingRecyclerView.setNestedScrollingEnabled(true);
nestedView = (NestedScrollView) findViewById(R.id.nestedView);
nestedView.setSmoothScrollingEnabled(true);
mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {

@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int countComment = mFirebaseAdapter.getItemCount();
int lastVisiblePosition = manager.findLastCompletelyVisibleItemPosition();

if (lastVisiblePosition == -1
|| (positionStart >= (countComment - 1) && lastVisiblePosition == (positionStart - 1))) {
commentingRecyclerView.smoothScrollToPosition(positionStart);
}

}
});

commentingRecyclerView.setLayoutManager(manager);
commentingRecyclerView.setAdapter(mFirebaseAdapter);

commentText = (EditText) findViewById(R.id.commentEditText);
commentText.addTextChangedListener(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) {
if (s.toString().trim().length() > 0) {
sendButton.setEnabled(true);
sendButton.setImageResource(R.drawable.ic_send_24dp);
} else {
sendButton.setEnabled(false);
sendButton.setImageResource(R.drawable.ic_comment_24dp);
}

// commentingRecyclerView.scrollTo(commentingRecyclerView.getBottom(),commentingRecyclerView.getBottom());
}

@Override
public void afterTextChanged(Editable s) {

}
});

mDatabaseUsers.child(firebaseUser.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mPhotoUrl = (String) dataSnapshot.child("image").getValue();
mCurrentUser = (String) dataSnapshot.child("name").getValue();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

sendButton = (ImageButton) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Comments newComment = new Comments(commentText.getText().toString().trim(), mCurrentUser, mPhotoUrl);
mCommentingDatabase.push().setValue(newComment);
commentText.setText("");
sendButton.setImageResource(R.drawable.ic_comment_24dp);
}
});

}


}
`

Answer

Actually.. the error was in the layout ..when you try to bind your layout in viewHolder and if there is any in custom layout then it throws the nullPointerException ..so in view add a class="com.example.myapp.Myclass" property