0

I am a beginner Android programmer.

I am creating a shop dialog for a little android game. Then decided to make each shop item a custom view/Group of views. I successfully made a class for it but am unable to show it in my xml layout file. When the shop dialog turns on, everything crashes.

custom view class:

public class ShopItem extends RelativeLayout {
    public ShopItem(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.shopcustomvielayout, this);
    }
}

Yes, I know that I should not use AbsoluteLayout, but since I am using a dialog it scales perfectly (it doesn't scale at all) and is most comfortable for my current purposes.

And here is the code I use in my dialog/shop layout file.

<AbsoluteLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="#E8E8E8"
    android:scrollbars="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true">
<view class="com.example.Example.MyActivity$ShopItem"
      android:id="@+id/ShopItem1"
      android:layout_width="fill_parent"
      android:layout_height="56dp"
      android:layout_x="5dp" android:layout_y="40dp"/>

</AbsoluteLayout>

All the other code I have should work perfectly fine, since I wrote it previously and it worked just fine.

I have been struggling for 3 days, can someone PLEASE HELP ME?

Thank you very much to everyone that helped me, I was able to figure out m porblem and my stupid mistake.

user2990508
  • 255
  • 1
  • 4
  • 11

2 Answers2

0

These kinds of errors are easiest to debug using the stack trace from logcat. Also, make sure you are certain you know what line is actually causing the crash. Attach the debugger and see.

The issue looks like it is due to you subclassing a RelativeLayout. I'm not sure exactly what you are trying to accomplish here, but if you want a dialog, use the dialog class and set its view to the view of your layout.

This documentation gives some good code snippets that explain how to use a custom layout for a dialog:http://developer.android.com/guide/topics/ui/dialogs.html

HoppedUpDev
  • 71
  • 1
  • 4
0

Apparently, I put my ShopItem class inside my MainActivity class. That was what made it crash.

Remember: Each class that is inside another class stays in that class and is only directly used in that class. But if you wish to access a class globally, then give it it's own file.

user2990508
  • 255
  • 1
  • 4
  • 11