0

I've a library included into my project, and want to define and element inside my XML file, this element type is from a class resides in this library, when I just declares it with the name it gives me an error through running :

  <ClassType
    android:id="@+id/my_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

and when just declares it as a View then cast it through run time I'm getting ClassCastException :

   <View
    android:id="@+id/my_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

then in Java,

ClassType my_view = (ClassType) findViewById(R.id.my_view);

UPDATE:

the total log when trying to add the whole namespace:

10-26 14:45:47.753: E/AndroidRuntime(1430): FATAL EXCEPTION: main
10-26 14:45:47.753: E/AndroidRuntime(1430): Process: com.lockscreen, PID: 1430
10-26 14:45:47.753: E/AndroidRuntime(1430): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lockscreen/com.lockscreen.LockScreenAppActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.ClassType
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.os.Looper.loop(Looper.java:136)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invoke(Method.java:515)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at dalvik.system.NativeStart.main(Native Method)
10-26 14:45:47.753: E/AndroidRuntime(1430): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.ClassType
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.Activity.setContentView(Activity.java:1929)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at com.lockscreen.LockScreenAppActivity.onCreate(LockScreenAppActivity.java:75)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.Activity.performCreate(Activity.java:5231)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-26 14:45:47.753: E/AndroidRuntime(1430):     ... 11 more
10-26 14:45:47.753: E/AndroidRuntime(1430): Caused by: java.lang.reflect.InvocationTargetException
10-26 14:45:47.753: E/AndroidRuntime(1430):     at java.lang.reflect.Constructor.constructNative(Native Method)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
10-26 14:45:47.753: E/AndroidRuntime(1430):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
10-26 14:45:47.753: E/AndroidRuntime(1430):     ... 22 more
10-26 14:45:47.753: E/AndroidRuntime(1430): Caused by: java.lang.NoClassDefFoundError: com.squareup.timessquare.R$styleable
10-26 14:45:47.753: E/AndroidRuntime(1430):     at com.example.ClassType.<init>(ClassType.java:99)
10-26 14:45:47.753: E/AndroidRuntime(1430):     ... 25 more
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118

2 Answers2

1

Put the whole namespace

 <com.library.namespace.ClassType
    android:id="@+id/my_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
pomber
  • 23,132
  • 10
  • 81
  • 94
  • how to get the whole namespace, it's a library included as a `jar` file into my libs folder. – Muhammed Refaat Oct 26 '14 at 14:42
  • You said you also tried to cast it, so you must know the namespace. For being able to cast it you added an `import`, that's the namespace you are looking for. – pomber Oct 26 '14 at 14:45
  • no I'm not mean I don't know the namespace, I knew it but how to make the XML get it. In java I just import the class with it's name space but when writing the whole namespace in xml it crashes and gives me `Error inflating class' upon running – Muhammed Refaat Oct 26 '14 at 14:49
  • The XML shouldn't have any problem with the namespace, even if it is from a library. Do you have more details of the error? – pomber Oct 26 '14 at 14:53
  • I added the full log obtained when adding the whole namespace to xml – Muhammed Refaat Oct 26 '14 at 15:09
  • It seems that the problem is the way you added the dependency. Try this http://stackoverflow.com/a/3643015/1325646. – pomber Oct 26 '14 at 15:21
  • there was a little problem I've in dependencies and I fixed according to that post, but I still get the same error as it is. – Muhammed Refaat Oct 27 '14 at 08:16
0

You have to use a Fully Qualified Name (FQN) in your xml

<com.example.ClassType
android:id="@+id/my_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Luca T.
  • 1,641
  • 1
  • 14
  • 18