FATAL EXCEPTION: main in Android studio - java

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.

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(), ...

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

Related

I am getting an runtime error (Null Pointer Exception) while using ListView in Android Studio

I am making a Task Manager app using ListView to Show all the tasks.But I am getting this error
Process: com.example.taskmanager, PID: 29293
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taskmanager/com.example.taskmanager.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.taskmanager.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:6024)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426) 
at android.app.ActivityThread.access$900(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5318) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717) 
what I think the error is that, I am sending wrong context in my custom adapter in MainActivity.java file
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.example.taskmanager.Model.DataClass;
import com.example.taskmanager.MyAdapter.MyAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<DataClass> datalist= new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
DataClass data1 = new DataClass();
data1.setId(1);
data1.setStatus(0);
data1.setTask("yo yo yo");
datalist.add(data1);
datalist.add(data1);
datalist.add(data1);
datalist.add(data1);
datalist.add(data1);
ListView taskListView = findViewById(R.id.listView);
MyAdapter myadapter = new MyAdapter(getApplicationContext(),R.layout.new_task_layout,datalist);
taskListView.setAdapter(myadapter);
}
}
My custom Adapter is
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.taskmanager.Model.DataClass;
import com.example.taskmanager.R;
import java.util.List;
public class MyAdapter extends ArrayAdapter<DataClass> {
Context mcontext;
int lytresourceId;
List<DataClass> data;
public MyAdapter(#NonNull Context context, int resource, #NonNull List<DataClass> objects) {
super(context, resource, objects);
this.mcontext=context;
this.lytresourceId=resource;
this.data=objects;
}
#Nullable
#Override
public DataClass getItem(int position) {
return super.getItem(position);
}
#NonNull
#Override
public View getView(int position, #Nullable View row, #NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mcontext);
row = inflater.inflate(lytresourceId,parent,false);
CheckBox checkbox;
checkbox=row.findViewById(R.id.checkBox);
DataClass mdata = data.get(position);
checkbox.setText(mdata.getTask());
return row;
}
}
My Xml file for activity Main is
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/TaskText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="Task"
android:textColor="#000000"
android:textSize="40sp"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/LinearLayout1"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="30dp"
android:layout_marginRight="20dp"
app:srcCompat="#android:drawable/ic_input_add" />
</RelativeLayout>
My custom layout file is
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="3dp"
app:cardCornerRadius="8dp"
android:layout_marginHorizontal="4dp"
android:layout_marginVertical="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="task1"
android:padding="4dp"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
My Data class is
package com.example.taskmanager.Model;
public class DataClass {
private int id,status;
private String task;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getTask() {
return task;
}
public void setTask(String task) {
this.task = task;
}
}
I am also using a Splash Screen but, I don't think that is a problem because my app is crashing after the splash screen is over, and when the intent goes to MainActivity.
Can someone please tell me the error
Also, I don't think there are any Dependencies missing from my File.
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
You are getting this error because setContentView(R.layout.layoutname) is missing in MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this is missing in your class
setContentView(R.layout.activity_main);
}
I think you are missing the setContentView(R.layout.activity_main) in the onCreate of MainActivity. Assuming activity_main is the xml file name. If not, replace it with actual name.
Due to this, your layout is never inflated and hence you should be getting null from findViewById(R.id.listView).

Recycler View Null Pointer Exception [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
**I am new to android studio and java
I'm trying to bind a button to an activity that contains a recycler view and the adapter that comes with it. The problem is when I click on the button the activity crashes. I know the button is not the problem because I have a different activity that uses a button and goes to the activity.
The problem is in either the activity for the recycler view which is named EdwardActivity or the adapter and I can't seem to figure it out.
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.edu.pfrfitness, PID: 10520
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edu.pfrfitness/com.edu.pfrfitness.EdwardActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at com.edu.pfrfitness.EdwardActivity.onCreate(EdwardActivity.java:31)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
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:2066) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
I/Process: Sending signal. PID: 10520 SIG: 9
Activity for recycler(named Edward Activity):
package com.edu.pfrfitness;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class EdwardActivity extends AppCompatActivity {
RecyclerView recyclerView;
String s1[], s2[];
int images [] = {R.drawable.black, R.drawable.black, R.drawable.black, R.drawable.black, R.drawable.black, R.drawable.black};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_row);
recyclerView = findViewById(R.id.recyclerView);
s1 = getResources() .getStringArray(R.array.Navigation_List);
s2 = getResources() .getStringArray(R.array.description);
MyAdapter myAdapter = new MyAdapter(this, s1, s2, images);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(myAdapter);
//call recycler
}
public void Recycler(){
}
}
Adapter:
package com.edu.pfrfitness;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
String data1[], data2[];
int images[];
Context context;
public MyAdapter(Context ct, String s1[], String s2[], int img[]) {
context = ct;
data1 = s1;
data2 = s2;
images = img;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.edwardText1.setText(data1[position]);
holder.edwardText2.setText(data2[position]);
holder.edwardImageView.setImageResource(images[position]);
}
#Override
public int getItemCount() {
return images.length;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView edwardText1, edwardText2;
ImageView edwardImageView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
edwardText1 = itemView.findViewById(R.id.edwardText1);
edwardText2 = itemView.findViewById(R.id.edwardText2);
edwardImageView = itemView.findViewById(R.id.edwardImageView);
}
}
}
My row so like how each row will look:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="wrap_content"
android:layout_margin="10dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/edwardImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#tools:sample/avatars" />
<TextView
android:id="#+id/edwardText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:text="Navigation_List"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/edwardImageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/edwardText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="description"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="#+id/edwardText1"
app:layout_constraintTop_toBottomOf="#+id/edwardText1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Error explanation:
According to the error you are trying to call setLayoutManager function on a null object. So, your variable "recyclerView" is obviously null.
Reason:
The reason is that in your activity you do this:
setContentView(R.layout.my_row);
and you use the file "my_row.xml" to create your Activity layout. But, in this layout there is no view with id recyclerView. So, when you do this:
recyclerView = findViewById(R.id.recyclerView);
then variable recyclerView is set to null.
Solution:
I guess that by mistake you used file "my_row.xml" as the Activity's layout, since this layout was obviously created fot the adapter's items. You should instead use another layout for the Activity's layout by passing it in your setContent in Activity's onCreate.

Error: Binary XML file line #9 in com.example.myfragment:layout/activity_main: Error inflating class fragment

I have been trying to put a recycler view inside a fragment and displaying the fragment in the main activity in Android Studio. It kept giving me and error inflating class fragment error. I don't know if it's the XML or the fragment code that contains the problems.
Error Code
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myfragment, PID: 11025
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfragment/com.example.myfragment.MainActivity}: android.view.InflateException: Binary XML file line #9 in com.example.myfragment:layout/activity_main: Binary XML file line #9 in com.example.myfragment:layout/activity_main: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.view.InflateException: Binary XML file line #9 in com.example.myfragment:layout/activity_main: Binary XML file line #9 in com.example.myfragment:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9 in com.example.myfragment:layout/activity_main: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Must specify unique android:id, android:tag, or have a parent with an id for com.example.myfragment.AddExerciseFragment
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3177)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:134)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:336)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:438)
at android.app.Activity.setContentView(Activity.java:3324)
at com.example.myfragment.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Here is my activity main
package com.example.myfragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<fragment
android:name="com.example.myfragment.AddExerciseFragment"
tools:layout="#layout/add_exercise_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Code
package com.example.myfragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class AddExerciseFragment extends Fragment{
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
MainActivity activity;
View view;
public AddExerciseFragment(MainActivity activity) {
this.activity = activity;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.add_exercise_fragment,container,false);
mRecyclerView = view.findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mAdapter = new ExampleAdapter(activity);
mRecyclerView.setAdapter(mAdapter);
return view;
}
}
Fragment XML
<?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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:padding="4dp"
android:scrollbars="vertical" />
</RelativeLayout>
Example Item
package com.example.myfragment;
import android.widget.Spinner;
public class ExampleItem {
private String mWorkoutName;
private String workoutNum;
private String munit;
private Spinner mUnitSpinner;
public ExampleItem(String workoutName, String num, String unit) {
mWorkoutName = workoutName;
workoutNum = num;
munit = unit;
}
public Spinner getmUnitSpinner() {
return mUnitSpinner;
}
public void setSpinner (Spinner _mUnitSpinner) {
mUnitSpinner = _mUnitSpinner;
}
public String getmWorkoutName() {
return mWorkoutName;
}
public String getWorkoutNum() {
return workoutNum;
}
public String getMunit() {
return munit;
}
public void setmWorkoutName(String mWorkoutName) {
this.mWorkoutName = mWorkoutName;
}
public void setWorkoutNum(String workoutNum) {
this.workoutNum = workoutNum;
}
public void setMunit(String munit) {
this.munit = munit;
}
}
Example Item XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
android:id="#+id/addworkoutcardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/structurespinner"
android:padding="5dp"
app:cardCornerRadius="4dp"
app:layout_constraintTop_toBottomOf="#+id/structurespinner"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/number"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toRightOf="#+id/workoutname"/>
<EditText
android:id="#+id/workoutname"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginEnd="19dp"
android:ems="10"
android:layout_marginRight="19dp" />
<Spinner
android:id="#+id/unitspinner"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="7dp"
android:layout_toRightOf="#+id/number"
android:entries="#array/workoutUnit"
android:layout_toEndOf="#+id/number"
android:layout_marginLeft="7dp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
Recycler View Adapter
package com.example.myfragment;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private Activity activity;
private AddExerciseController controller;
public ExampleAdapter(MainActivity activity) {
this.activity = activity;
this.controller = AddExerciseController.getController();
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public EditText workoutName;
public EditText workoutNum;
public Spinner workoutUnit;
public ExampleViewHolder(View itemView) {
super(itemView);
workoutName = itemView.findViewById(R.id.workoutname);
workoutNum = itemView.findViewById(R.id.number);
workoutUnit = itemView.findViewById(R.id.unitspinner);
workoutUnit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
String sStep = adapter.getItemAtPosition(position).toString();
workoutUnit.setSelection(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
#Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
#Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = controller.getListExercise().get(position);
holder.workoutName.setText(currentItem.getmWorkoutName());
holder.workoutNum.setText(currentItem.getWorkoutNum());
currentItem.setSpinner(holder.workoutUnit);
// store the spinner in the DetailItem object
currentItem.setSpinner(holder.workoutUnit);
// store DetailItem object in the array
controller.getListExercise().set(position, currentItem);
}
#Override
public int getItemCount() {
return controller.getListExercise().size();
}
Exercise Controller
package com.example.myfragment;
import java.util.ArrayList;
import java.util.List;
public class AddExerciseController {
private List<ExampleItem> exampleList;
private static AddExerciseController controller;
public AddExerciseController(){
initMovieList();
}
private List<ExampleItem> initMovieList(){
if (this.exampleList == null) {
this.exampleList = new ArrayList<ExampleItem>();
exampleList.add(new ExampleItem("workout1","60", "seconds"));
exampleList.add(new ExampleItem("workout2","50","seconds"));
exampleList.add(new ExampleItem("workout3","5","seconds"));
}
return this.exampleList;
}
public List<ExampleItem> getListExercise() {
return exampleList;
}
public static AddExerciseController getController(){
if(controller == null){
controller = new AddExerciseController();
}
return controller;
}
}
The error message says:
Caused by: java.lang.IllegalArgumentException: Binary XML file line #9:
Must specify unique android:id, android:tag, or have a parent with an id
for com.example.myfragment.AddExerciseFragment
And if we look at your activity XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<fragment
android:name="com.example.myfragment.AddExerciseFragment"
tools:layout="#layout/add_exercise_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You'll note that your <fragment> tag does not have an android:id, android:tag, nor does your parent layout have an android:id. That's why you're getting the exception.
Therefore you can fix this by adding an android:id to your <fragment> tag (you also don't want to be using a ConstraintLayout with wrap_content sizes with a fragment - you want just a simple FrameLayout):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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=".MainActivity">
<fragment
android:id="#+id/add_exercise_fragment"
android:name="com.example.myfragment.AddExerciseFragment"
tools:layout="#layout/add_exercise_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
It should be noted that Fragments must always have an empty constructor, so this code snippet is going to cause you to crash immediately afterwards:
public AddExerciseFragment(MainActivity activity) {
this.activity = activity;
}
You should replace that constructor with a constructor with no parameters:
public AddExerciseFragment() {
}
You'll want to use getActivity() to retrieve the Activity, casting it to your MainActivity as needed.
I have got the same error but it was because of different issue.I would like to add this answer here.
The same error is caused when you fail to mention a serializable data class in kotlin android room database.
data class ..(...):Serializable(check whether your data class is Serializaable)

SOLVED: App with TabLayout with FragmentPagerAdapter and ViewPager crashes

Solution: updated dependecies
I want to make an app with three tabs, setup with a ViewPager and TabLayout. I'm using FragmentPagerAdapter. Layouts and activities are error-free, but still the app crashes. Activities, layouts and crashreport below.
2019-08-11 14:03:15.791 25598-25598/ga.rndevelopment.inventory E/AndroidRuntime: FATAL EXCEPTION: main
Process: ga.rndevelopment.inventory, PID: 25598
java.lang.BootstrapMethodError: Exception from call site #42 bootstrap method
at com.google.android.material.tabs.TabLayout$TabView.addOnLayoutChangeListener(TabLayout.java:2592)
at com.google.android.material.tabs.TabLayout$TabView.update(TabLayout.java:2508)
at com.google.android.material.tabs.TabLayout$TabView.setTab(TabLayout.java:2437)
at com.google.android.material.tabs.TabLayout.createTabView(TabLayout.java:1501)
at com.google.android.material.tabs.TabLayout.newTab(TabLayout.java:855)
at com.google.android.material.tabs.TabLayout.populateFromPagerAdapter(TabLayout.java:1477)
at com.google.android.material.tabs.TabLayout.setPagerAdapter(TabLayout.java:1468)
at com.google.android.material.tabs.TabLayout.setupWithViewPager(TabLayout.java:1379)
at com.google.android.material.tabs.TabLayout.setupWithViewPager(TabLayout.java:1340)
at com.google.android.material.tabs.TabLayout.setupWithViewPager(TabLayout.java:1320)
at ga.rndevelopment.inventory.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:7458)
at android.app.Activity.performCreate(Activity.java:7448)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3382)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3587)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2185)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7593)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Caused by: java.lang.ClassCastException: Bootstrap method returned null
at com.google.android.material.tabs.TabLayout$TabView.addOnLayoutChangeListener(TabLayout.java:2592) 
at com.google.android.material.tabs.TabLayout$TabView.update(TabLayout.java:2508) 
at com.google.android.material.tabs.TabLayout$TabView.setTab(TabLayout.java:2437) 
at com.google.android.material.tabs.TabLayout.createTabView(TabLayout.java:1501) 
at com.google.android.material.tabs.TabLayout.newTab(TabLayout.java:855) 
at com.google.android.material.tabs.TabLayout.populateFromPagerAdapter(TabLayout.java:1477) 
at com.google.android.material.tabs.TabLayout.setPagerAdapter(TabLayout.java:1468) 
at com.google.android.material.tabs.TabLayout.setupWithViewPager(TabLayout.java:1379) 
at com.google.android.material.tabs.TabLayout.setupWithViewPager(TabLayout.java:1340) 
at com.google.android.material.tabs.TabLayout.setupWithViewPager(TabLayout.java:1320) 
at ga.rndevelopment.inventory.MainActivity.onCreate(MainActivity.java:21) 
at android.app.Activity.performCreate(Activity.java:7458) 
at android.app.Activity.performCreate(Activity.java:7448) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3382) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3587) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2185) 
at android.os.Handler.dispatchMessage(Handler.java:112) 
at android.os.Looper.loop(Looper.java:216) 
at android.app.ActivityThread.main(ActivityThread.java:7593) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 
2019-08-11 14:03:15.810 25598-25598/ga.rndevelopment.inventory I/Process: Sending signal. PID: 25598 SIG: 9
MainActivity
package ga.rndevelopment.inventory;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.viewPager);
setupViewPager(viewPager);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
adapter.addFragment(new TabOne(), "ONE");
adapter.addFragment(new TabTwo(), "TWO");
adapter.addFragment(new TabThree(), "THREE");
viewPager.setAdapter(adapter);
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
android:layout_gravity="top" />
</androidx.viewpager.widget.ViewPager>
</RelativeLayout>
PagerAdapter
package ga.rndevelopment.inventory;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> FragmentList = new ArrayList<>();
private final List<String> FragmentTitleList = new ArrayList<>();
public PagerAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
#Override
public Fragment getItem(int position) {
return FragmentList.get(position);
}
#Override
public int getCount() {
return FragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
FragmentList.add(fragment);
FragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return FragmentTitleList.get(position);
}
}
TabOne
package ga.rndevelopment.inventory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class TabOne extends Fragment {
public TabOne() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_standard, container, false);
}
}
As Android Studio doesn't throw an error anywhere, I don't know in which class I need to search for eventual missing parts of code. Anyone some reproduction ideas?
Thanks in advance
Caused by: java.lang.ClassCastException: Bootstrap method returned
null
work on this one...casting goes wrong with null values.

App is crashing while switching activitys, build is complete, no error

My app is crashing while switching from the after login activity to the in app´s profile activity.
Build is complete without errors.
Any ideas why? (Thankful for every Help)
//////////////////////////////////////////////////////////////////////////
ProfileActivity ---->
package com.smarthelp.smarthelp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import com.google.firebase.auth.FirebaseAuth;
public class ProfileActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private Button logout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
firebaseAuth= FirebaseAuth.getInstance();
logout = (Button)findViewById(R.id.btnLogout);
logout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Logout();
}
});
configureibtnHelp();
}
private void Logout(){
firebaseAuth.signOut();
finish();
startActivity(new Intent(ProfileActivity.this, LoginActivity.class));
}
private void configureibtnHelp() {
ImageButton ibtnHelp = (ImageButton) findViewById(R.id.ibtnHelp);
ibtnHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.profile_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.logoutMenu:{
Logout();
}
}
return super.onOptionsItemSelected(item);
}
}
///////////////////////////////////////////////////////////////////////
HelpActivity ---->
package com.smarthelp.smarthelp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import com.google.firebase.auth.FirebaseAuth;
public class HelpActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
configureibtnProfile();
}
private void configureibtnProfile(){
ImageButton ibtnProfile = (ImageButton) findViewById(R.id.ibtnProfile);
ibtnProfile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(HelpActivity.this, ProfileActivity.class));
}
});
}
}
//////////////////////////////////////////////////////////////////////////////
activity_help.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".HelpActivity">
<ImageButton
android:id="#+id/ibtnProfile"
android:layout_width="51dp"
android:layout_height="52dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="ProfilButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.025"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983"
app:srcCompat="#drawable/profil" />
<ImageButton
android:id="#+id/ibtnOrder"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="OrderButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983"
app:srcCompat="#drawable/order" />
<ImageButton
android:id="#+id/ibtnHelp"
android:layout_width="51dp"
android:layout_height="53dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="236dp"
android:layout_marginRight="8dp"
android:layout_marginStart="236dp"
android:layout_marginTop="8dp"
android:contentDescription="HelpButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.875"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983"
app:srcCompat="#drawable/help" />
</android.support.constraint.ConstraintLayout>
///////////////////////////////////////////////////////////////////////////////
------->Logcat
08-13 19:13:15.273 21068-21068/com.smarthelp.smarthelp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smarthelp.smarthelp, PID: 21068
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smarthelp.smarthelp/com.smarthelp.smarthelp.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.smarthelp.smarthelp.ProfileActivity.configureibtnHelp(ProfileActivity.java:48)
at com.smarthelp.smarthelp.ProfileActivity.onCreate(ProfileActivity.java:35)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
From the crash log it seems
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
there is an NPE on the button for which you are trying to set onclick listener.
In your ProfileActivity set the button to one of the ImageButtons of XML
Assuming you are trying to do for first of your image buttons
just add the following line before setting onClickListener
logout=(Button) findViewById(R.id.ibtnProfile);
Change
firebaseAuth.getInstance();
to
FirebaseAuth.getInstance();
at line 14 in ProfileActivity.java
It's pretty much said in the stacktrace: trying to call addOnClickListener on a null object at ProfileActivity on line 27
You haven't initialized logout button with any value. Probably forgot to findViewById it.

Categories

Resources