0

i programming a code that its goal is set the arraylist items to a recycleview. but when i run the project, the app will crashing.

d.

i check dependecies. they be ok. and i check the codes and i think they don't have any issue.

//MainActivityClasss

public class MainActivity extends AppCompatActivity {

RecyclerView rcl_cities;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initViews();
    Log.e("ApplicationSays:","LayoutManager set");
}

private void initViews(){

    rcl_cities = findViewById(R.id.rcl_cities);

    rcl_cities.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));

    CityAdapter cityAdapter = new CityAdapter(this,getCities());
    rcl_cities.setAdapter(cityAdapter);

}

private ArrayList<City> getCities()
{

    ArrayList<City> cities = new ArrayList<>();

    cities.add(new City(1, "mashhad"));
    cities.add(new City(2, "Tehran"));
    cities.add(new City(3, "Gorgan"));
    cities.add(new City(4, "Shiraz"));
    cities.add(new City(5, "Rasht"));

    return cities;
}
}

//CityClass:

public class City {

private int id;
private String name;

public City() {
}

public City(int id, String name) {
    this.id = id;
    this.name = name;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

//CityViewHolderClass:

public class CityViewHolder extends RecyclerView.ViewHolder {
public TextView txtCityName;
public CityViewHolder(@NonNull View itemView) {
    super(itemView);
    txtCityName = itemView.findViewById(R.id.txt_city_name);
}
}

//CityAdapterClass:

public class CityAdapter extends RecyclerView.Adapter<CityViewHolder> {
private Context context;
private ArrayList<City> cities;
public CityAdapter(Context context, ArrayList<City> cities) {
    this.context = context;
    this.cities = cities;
}

@NonNull


@Override
public CityViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_city,parent,false);
    CityViewHolder cityViewHolder = new CityViewHolder(view);
    return cityViewHolder;
}

@Override
public void onBindViewHolder(@NonNull CityViewHolder holder, int position) {

    City city = cities.get(position);
    holder.txtCityName.setText(city.getName());

}

@Override
public int getItemCount() {
    if(cities == null){
        return 0;
    }
    return cities.size();
}
}

//main layout:

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=".Activities.MainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rcl_cities"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>

//recycler layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical">

    <TextView
        android:id="@+id/txt_city_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="city"
        android:textSize="14sp"
        android:textColor="#5f5f5e"
        android:textStyle="bold"
        android:padding="14dp"/>

</LinearLayout>

<view
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="@android:color/holo_red_dark" />

</LinearLayout>

these are my errors after app crashed:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 5282
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:715)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    at com.example.myapplication.Adapters.CityAdapter.onCreateViewHolder(CityAdapter.java:30)
    at com.example.myapplication.Adapters.CityAdapter.onCreateViewHolder(CityAdapter.java:17)
    at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
    at android.view.View.layout(View.java:15596)
    at android.view.ViewGroup.layout(ViewGroup.java:4966)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
    at android.view.View.layout(View.java:15596)
    at android.view.ViewGroup.layout(ViewGroup.java:4966)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
    at android.view.View.layout(View.java:15596)
    at android.view.ViewGroup.layout(ViewGroup.java:4966)
    at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
    at android.view.View.layout(View.java:15596)
    at android.view.ViewGroup.layout(ViewGroup.java:4966)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
    at android.view.View.layout(View.java:15596)
    at android.view.ViewGroup.layout(ViewGroup.java:4966)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
    at android.view.View.layout(View.java:15596)
    at android.view.ViewGroup.layout(ViewGroup.java:4966)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
    at android.view.View.layout(View.java:15596)
    at android.view.ViewGroup.layout(ViewGroup.java:4966)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
    at android.view.Choreographer.doFrame(Choreographer.java:550)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • You got layout inflation error, so I think it's very likely related to a typo in your XML layout, try ` – Aaron Oct 23 '19 at 22:30
  • Did you try debugging your code? Also, did you check for typos? Red squiggly lines or any compile time error? – Shankha057 Oct 23 '19 at 22:35
  • see [Debug with Android Studio](https://developer.android.com/studio/debug), in particular the part with setting break-points... you can try that eg. with method `.onBindViewHolder()`. One can inspect all values alike that - where one of them is `null`. – Martin Zeitler Oct 23 '19 at 23:47

1 Answers1

1

First of all, change your view in xml to View

<View
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="@android:color/holo_red_dark" />

Secondly, You are not doing something right in your onBindViewHolder method

City city = cities.get(position);

City should be declared and initialized an instance of it before attempting to passing value to it.

City city;
//....

city = new City
//...

city.setId(cities.get(position));

With this you can test your code again.

olajide
  • 972
  • 1
  • 13
  • 26
  • 1
    its work. But recyclerview don't list the items in UI and just show one item on above. so i change this code: `View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_city,parent,false);` to: `View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_city,null);` and: **Application Running Successfully And List The Items.** – Morteza Hosseini Oct 24 '19 at 08:59
  • Glad it worked for you. – olajide Oct 24 '19 at 12:01