i'm new to android studio, and i'm currently trying to create an app that shows a recycler view of tourist attractions after switching to a new activity.
However, when i try to switch to the new activity with the recycler view from the landing page, the app crashes and i'm not sure why, any help and suggestions would be greatly appreciated. Thank you!
PS: There are no error messages and everything builds fine, so I'm not sure where to start
Main Activity code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btEnter);
//Enter into app main function button
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, TopLocationList.class);
startActivity(intent);
}
});
}
List of Locations activity code
public class TopLocationList extends AppCompatActivity {
RecyclerView recyclerView;
String s1[], s2[];
int images[] = {R.drawable.sydneyoperahouse, R.drawable.bondi, R.drawable.botanicgardens, R.drawable.harbourbridgeclimb,
R.drawable.rocks, R.drawable.thethreesisters, R.drawable.spicealley, R.drawable.thequay, R.drawable.mrwong,
R.drawable.thegrounds, R.drawable.manly};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_location_list);
recyclerView = findViewById(R.id.recyclerView);
s1 = getResources().getStringArray(R.array.top_location_names);
s2 = getResources().getStringArray(R.array.location_suburb);
AdapterClass adapterClass = new AdapterClass(this, s1, s2, images);
recyclerView.setAdapter(adapterClass);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
Adapter class code
public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder> {
String data1[], data2[];
int images[];
Context context;
public AdapterClass(Context ct, String s1[], String s2[], int img[]){
context = ct;
data1 = s1;
data2 = s2;
images = img;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.recyclerviewrows, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.mAttractionSuburb.setText(data1[position]);
holder.mAttractionName.setText(data2[position]);
holder.mImage.setImageResource(images[position]);
}
@Override
public int getItemCount() {
return data1.length;
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView mAttractionName, mAttractionSuburb;
ImageView mImage;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
mAttractionName = itemView.findViewById(R.id.tvAttractionName);
mAttractionSuburb = itemView.findViewById(R.id.tvAttractionSuburb);
mImage = itemView.findViewById(R.id.ivAttractionImage);
}
}
Strings.xml code (i added a string array through here)
<string name="app_name">TourismGuide</string>
<string-array name = "top_location_names">
<item> Sydney Opera House </item>
<item> Bondi Beach </item>
<item> Royal Botanic Garden </item>
<item> Harbour Bridge </item>
<item> The Rocks </item>
<item> Three Sisters Walk </item>
<item> Taronga Zoo </item>
<item> Spice Alley </item>
<item> The Quay </item>
<item> Mr Wong </item>
<item> The Grounds of Alexandria </item>
<item> Manly Beach </item>
</string-array>
<string-array name="location_suburb">
<item> Sydney </item>
<item> Bondi </item>
<item> Sydney </item>
<item> Sydney </item>
<item> Sydney </item>
<item> Katoomba </item>
<item> Mosman </item>
<item> Chippendale </item>
<item> Sydney </item>
<item> Wynyard </item>
<item> Alexandria </item>
<item> Manly </item>
</string-array>
I'm really sorry if this is long, again any help is greatly appreciated. Thanks!