Home > Back-end >  My app is crashing when i am trying to move from one activity to another activity due to java.lang.N
My app is crashing when i am trying to move from one activity to another activity due to java.lang.N

Time:09-23

This is my first android project. I don't know much about android development. Please give me a detailed solution. I am trying to move from my login activity to recyclerview activity but after clicking on login button my application is crashing.

This is log cat

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
    at android.view.LayoutInflater.from(LayoutInflater.java:284)
    at com.example.trackmybusadmin.recyclerAdapter.onCreateViewHolder(recyclerAdapter.java:27)
    at com.example.trackmybusadmin.recyclerAdapter.onCreateViewHolder(recyclerAdapter.java:13)
    at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
    at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
    at android.view.View.layout(View.java:22088)
    at android.view.ViewGroup.layout(ViewGroup.java:6325)
    at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
    at android.view.View.layout(View.java:22088)
    at android.view.ViewGroup.layout(ViewGroup.java:6325)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
    at android.view.View.layout(View.java:22088)
    at android.view.ViewGroup.layout(ViewGroup.java:6325)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
    at android.view.View.layout(View.java:22088)
    at android.view.ViewGroup.layout(ViewGroup.java:6325)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
    at android.view.View.layout(View.java:22088)
    at android.view.ViewGroup.layout(ViewGroup.java:6325)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
    at android.view.View.layout(View.java:22088)
    at android.view.ViewGroup.layout(ViewGroup.java:6325)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:812)
    at android.view.View.layout(View.java:22088)
    at android.view.ViewGroup.layout(ViewGroup.java:6325)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3191)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2701)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1819)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7781)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1031)
    at android.view.Choreographer.doCallbacks(Choreographer.java:854)
    at android.view.Choreographer.doFrame(Choreographer.java:789)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1016)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7590)

This is my recylerAdapter.java file

public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.MyViewHolder> {
String data1[],data2[],data3[];
int img[];
Context context;
public recyclerAdapter(Context ct, String title[],String description[],String count[], int imageicons[]){
data1=title;
data2=description;
data3=count;
img=imageicons;

}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.additembutton,parent,false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.text1.setText(data1[position]);
    holder.text2.setText(data2[position]);
    holder.text3.setText(data3[position]);
    holder.img1.setImageResource(img[position]);

}

@Override
public int getItemCount() {
    return data1.length;
}

public class MyViewHolder extends RecyclerView.ViewHolder{
    TextView text1,text2,text3;
    ImageView img1;
    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        text1 = itemView.findViewById(R.id.textViewtitle);
        text2 = itemView.findViewById(R.id.textViewdescription);
        text3 = itemView.findViewById(R.id.textViewCount);
        img1 = itemView.findViewById(R.id.imageView);
    }
}

}

This is my MainActivity2.java

public class MainActivity2 extends AppCompatActivity {
String title[],description[],count[];
int imageicons[] = {R.drawable.bus,R.drawable.student};
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    title = getResources().getStringArray(R.array.Title);
    description = getResources().getStringArray(R.array.Description);
    count = getResources().getStringArray(R.array.Count);
    recyclerView = findViewById(R.id.recycleview);

    recyclerAdapter ra = new recyclerAdapter(this, title, description,count,imageicons);
    recyclerView.setAdapter(ra);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));


}

}

CodePudding user response:

You should use:

LayoutInflater inflater = LayoutInflater.from(parent.getContext());

instead of:

LayoutInflater inflater = LayoutInflater.from(context);

CodePudding user response:

it seems that you have in the new activity a recycler view and inside onCreateViewHolder method you trying to getSystemService with using null context .. so if you can provide the code for that and how you are get context from the recycler view adapter

  • Related