-3

Every One I am trying to implement Recycler View in Android Studio, The code is not giving any error but when I test the app in my my phone it crashes. "Unfortunately the app stopped" that comes up when I open the app.

Can some one tell my the error in my code, It will be of great help. Here is my code:

Activity.Java file:

       package com.labtasks.hp.labtasks;

       import android.support.v7.app.AppCompatActivity;
       import android.os.Bundle;
       import android.support.v7.widget.LinearLayoutManager;
       import android.support.v7.widget.RecyclerView;

         public class ListActivity extends AppCompatActivity {

        @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RecyclerView rl=findViewById(R.id.list);
    rl.setLayoutManager(new LinearLayoutManager(this));
    String[] languges={"Ahmad","Ali","Jabed","Ross","Usama","Hasher","Ron","Bashir","G","Abuba","Asad","Ahsan","Hadi","Muneeb","Omar","Zaaem"};
    rl.setAdapter(new Adaptor(languges));
}
        }

Adaptor.java file:

    package com.labtasks.hp.labtasks;

       import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.Adapter;
   import android.widget.ImageView;
   import android.widget.TextView;
   import android.widget.Toast;



   public class Adaptor extends RecyclerView.Adapter<Adaptor.viewholder>                                         {
         private String[] data;

        public Adaptor(String[] data)
           {
            this.data=data;
          }


         @Override
         public viewholder onCreateViewHolder(ViewGroup parent, int           viewType) {
              LayoutInflater                inflater=LayoutInflater.from(parent.getContext());
             View view=inflater.inflate(R.layout.list_item,parent, false);
             return new viewholder(view);
           }

              @Override
              public void onBindViewHolder(viewholder holder, final int position) {
    String title=data[position];
    holder.t.setText(title);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(view.getContext(), "Item is clicked", Toast.LENGTH_SHORT).show();
        }
    });

}

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

public class viewholder extends RecyclerView.ViewHolder{
    ImageView i;
    TextView t;
    public viewholder(View itemView) {
        super(itemView);
        i=itemView.findViewById(R.id.img);
        t=itemView.findViewById(R.id.main);
    }
}

}

Executing tasks: [:app:assembleDebug]

:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:checkDebugManifest UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:prepareLintJar UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:createDebugCompatibleScreenManifests UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:splitsDiscoveryTaskDebug UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:javaPreCompileDebug UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk NO-SOURCE
:app:compileDebugSources UP-TO-DATE
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:transformClassesWithDexBuilderForDebug UP-TO-DATE
:app:transformDexArchiveWithExternalLibsDexMergerForDebug UP-TO-DATE
:app:transformDexArchiveWithDexMergerForDebug UP-TO-DATE
:app:mergeDebugJniLibFolders UP-TO-DATE
:app:transformNativeLibsWithMergeJniLibsForDebug UP-TO-DATE
:app:processDebugJavaRes NO-SOURCE
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
:app:validateSigningDebug
:app:packageDebug UP-TO-DATE
:app:assembleDebug UP-TO-DATE

BUILD SUCCESSFUL in 1s
25 actionable tasks: 1 executed, 24 up-to-date
Executing tasks: [:app:assembleDebug]

:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:checkDebugManifest UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:prepareLintJar UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:createDebugCompatibleScreenManifests UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:splitsDiscoveryTaskDebug UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:javaPreCompileDebug UP-TO-DATE
:app:compileDebugJavaWithJavac
:app:compileDebugNdk NO-SOURCE
:app:compileDebugSources
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:transformClassesWithDexBuilderForDebug
:app:transformDexArchiveWithExternalLibsDexMergerForDebug
:app:transformDexArchiveWithDexMergerForDebug
:app:mergeDebugJniLibFolders UP-TO-DATE
:app:transformNativeLibsWithMergeJniLibsForDebug UP-TO-DATE
:app:processDebugJavaRes NO-SOURCE
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
:app:validateSigningDebug
:app:packageDebug
:app:assembleDebug

BUILD SUCCESSFUL in 1s
25 actionable tasks: 6 executed, 19 up-to-date
Executing tasks: [:app:assembleDebug]

:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:checkDebugManifest UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:prepareLintJar UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources
:app:createDebugCompatibleScreenManifests UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:splitsDiscoveryTaskDebug UP-TO-DATE
:app:processDebugResources
:app:generateDebugSources
:app:javaPreCompileDebug UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk NO-SOURCE
:app:compileDebugSources UP-TO-DATE
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:transformClassesWithDexBuilderForDebug UP-TO-DATE
:app:transformDexArchiveWithExternalLibsDexMergerForDebug UP-TO-DATE
:app:transformDexArchiveWithDexMergerForDebug UP-TO-DATE
:app:mergeDebugJniLibFolders UP-TO-DATE
:app:transformNativeLibsWithMergeJniLibsForDebug UP-TO-DATE
:app:processDebugJavaRes NO-SOURCE
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
:app:validateSigningDebug
:app:packageDebug UP-TO-DATE
:app:assembleDebug UP-TO-DATE

BUILD SUCCESSFUL in 8s
25 actionable tasks: 3 executed, 22 up-to-date

ListActivity.XML

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.RecyclerView
   xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
     </android.support.v7.widget.RecyclerView>

List_Items activity.XML

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="8dp">
      <ImageView
      android:id="@+id/img"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@mipmap/ic_launcher"
      />
    <TextView
      android:id="@+id/text"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_weight="1"
         android:textSize="24dp"
          android:layout_gravity="center"
           android:paddingLeft="16dp"
         android:text="@string/app_name"/>
       </LinearLayout>
usama97
  • 33
  • 1
  • 6

1 Answers1

0

I have tested your code and it works. I think that your error is in your ListActivity Layout. Have you set an Id to the RecyclerView?

<android.support.v7.widget.RecyclerView
      android:id="@+id/list"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

After saw your xml files, I can see that you have initcialicate your RecyclerView with a wrong ID

Activity.Java file:

RecyclerView rl=findViewById(R.id.list);

ListActivity.XML

<android.support.v7.widget.RecyclerView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/list_item"
   android:layout_height="match_parent"
   android:layout_width="match_parent">
</android.support.v7.widget.RecyclerView>

Must be RecyclerView rl=findViewById(R.id.list_item);