Hey Everyone this is my first android app I'm new in Android development. When I try to run this app on my device suddenly it crashes.
And my app crash when I try to log in
This is the main code of JAVA
package com.example.javanots;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirestoreRegistrar;
import com.google.firebase.firestore.Query;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import java.text.BreakIterator;
public class NotesActivity extends AppCompatActivity {
FloatingActionButton mcreatenotefab;
private FirebaseAuth firebaseAuth;
RecyclerView mrecyclerview;
StaggeredGridLayoutManager staggeredGridLayoutManager;
FirebaseUser firebaseUser;
FirebaseFirestore firebaseFirestore;
FirestoreRecyclerAdapter<firebasemodel,NoteViewHolder> noteAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
mcreatenotefab=findViewById(R.id.createnotefab);
firebaseAuth=FirebaseAuth.getInstance();
//Get Data From Particular User Not from All user
firebaseUser=FirebaseAuth.getInstance().getCurrentUser();
firebaseFirestore=FirebaseFirestore.getInstance();
getSupportActionBar().setTitle("All Notes");
mcreatenotefab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(NotesActivity.this,Createnote.class));
}
});
//For Get All notes of User
Query query = firebaseFirestore.collection("notes").document(firebaseUser.getUid()).collection("mynotes").orderBy("title",Query.Direction.ASCENDING);
FirestoreRecyclerOptions<firebasemodel> allusernotes = new FirestoreRecyclerOptions.Builder<firebasemodel>().setQuery(query,firebasemodel.class).build();
noteAdapter = new FirestoreRecyclerAdapter<firebasemodel, NoteViewHolder>(allusernotes) {
@Override
protected void onBindViewHolder(@NonNull NoteViewHolder noteViewHolder, int position, @NonNull firebasemodel firebasemodel) {
noteViewHolder.notetitle.setText(firebasemodel.getTitle());
noteViewHolder.notecontent.setText(firebasemodel.getContent());
}
@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notes_layout,parent,false);
return new NoteViewHolder(view);
}
};
setContentView(R.layout.notes_layout);
mrecyclerview.findViewById(R.id.recyclerview);
mrecyclerview.setHasFixedSize(true);
staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mrecyclerview.setLayoutManager(staggeredGridLayoutManager);
mrecyclerview.setAdapter(noteAdapter);
}
public class NoteViewHolder extends RecyclerView.ViewHolder
{
private TextView notetitle;
private TextView notecontent;
LinearLayout mnote;
public NoteViewHolder(@NonNull View itemView) {
super(itemView);
notetitle=itemView.findViewById(R.id.notetitel);
notecontent=itemView.findViewById(R.id.notecontent);
mnote=itemView.findViewById(R.id.note);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.logout: firebaseAuth.signOut();
finish();
startActivity(new Intent(NotesActivity.this,MainActivity.class));
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
super.onStart();
noteAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
noteAdapter.startListening();
if(noteAdapter!=null)
{
noteAdapter.startListening();
}
}
}
This is my XML codes
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NotesActivity">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@ id/createnotefab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|end"
android:layout_marginRight="30dp"
android:layout_marginBottom="30dp"
android:background="#A1F4FB"
android:src="@drawable/ic_baseline_add_24"
app:maxImageSize="40dp">
</com.google.android.material.floatingactionbutton.FloatingActionButton>
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scrollbars="vertical">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
I'm getting a List of Error I don't know how to solve it
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.javanots, PID: 24241
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.javanots/com.example.javanots.NotesActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3835)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference
at com.example.javanots.NotesActivity.onCreate(NotesActivity.java:86)
at android.app.Activity.performCreate(Activity.java:8207)
at android.app.Activity.performCreate(Activity.java:8191)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3808)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Please Explain My problem's solution.
CodePudding user response:
You are trying to call "findViewById" on mrecyclerview, that's not initialized yet (it's value is null).
It seems you are trying to do the right (bind the recyclerview), but the wrong way.
Give it a try by changing
mrecyclerview.findViewById(R.id.recyclerview);
to
mrecyclerview = (RecyclerView) findViewById(R.id.recyclerview);
After that, you'll be able to call methods on recyclerview, like .setAdapter(), ...
CodePudding user response:
You had declared an object by the name mrecyclerview
right? In line 86 of your code, instead of mrecyclerview.findViewById(R.id.recyclerview)
, try mrecyclerview = findViewById(R.id.recyclerview)
. Your error lines show that the problem was caused by a NullPointerException - Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference