由于 java.lang.NullPointerException,当我尝试从一个 activity 移动到另一个 activity 时,我的应用程序崩溃了

My app is crashing when i am trying to move from one activity to another activity due to java.lang.NullPointerException

这是我的第一个 android 项目。我对 android 开发了解不多。请给我一个详细的解决方案。我正在尝试从我的登录 activity 移动到 recyclerview activity 但是在单击登录按钮后我的应用程序崩溃了。

这是日志猫

    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)

这是我的recylerAdapter.java文件

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);
    }
}

}

这是我的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));


}

}

您似乎在新的 activity 中有一个回收器视图,并且在 onCreateViewHolder 方法中您尝试 getSystemService使用空上下文 .. 所以如果你能提供代码以及你如何从回收器视图适配器中获取上下文

你应该使用:

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

而不是:

LayoutInflater inflater = LayoutInflater.from(context);