Это мой первый андроид-проект. Я мало что знаю о разработке под Android. Пожалуйста, дайте мне подробное решение. Я пытаюсь перейти от активности входа в систему к активности recyclerview, но после нажатия кнопки входа в систему мое приложение вылетает.

Это журнал кота

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


}

}

-1
Deepak Chauhan 23 Сен 2021 в 13:23

2 ответа

Лучший ответ

Вам следует использовать:

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

Вместо того:

LayoutInflater inflater = LayoutInflater.from(context);
1
Akhil 23 Сен 2021 в 10:45

Похоже, что в новом действии у вас есть представление ресайклера и внутри метода onCreateViewHolder , который вы пытаетесь выполнить getSystemService с использованием нулевого контекста ... поэтому, если вы можете предоставить код для этого и как вы получаете контекст из адаптера просмотра ресайклера

0
Mohamed Saleh 23 Сен 2021 в 10:34