Can't find custom view

  • Replies:15
David Williams
  • Forum posts: 14

May 16, 2016, 10:22:07 PM via Website

I'm trying to create a custom view.
I create a class file View subclass.
And I add it to the XML file for the interface.
It says the class file can't be found.
And when I compile it says "view cannot be instantiated" and crashes.

Do I have to add some sort of import com.me.app.MyView in the XML interface file?

Where do I add the include statement?

Some kind of include in the MainActivity java file?

damponting44George Spils

Reply
Vladimir S.
  • Forum posts: 266

May 16, 2016, 10:52:32 PM via Website

Hi David, welcome to AndroidPIT forum!

In the xml you need to name custom view with full package name, for example:

    <com.me.app.MyView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_View"
    .............................
    />

MainActivity onCreate() must include something like this:

    MyView myView = new MyView(this);
    myView = (MyView) findViewById(R.id.my_View);

— modified on May 16, 2016, 11:00:51 PM

George Spils

Reply
David Williams
  • Forum posts: 14

May 16, 2016, 11:46:35 PM via Website

Its still giving the "class not found" warning and crashing.

Should MainActivity.java say

import com.domain.me.app.MyView

What about MyView.java

package com.domain.me.app

Or

package com.domain.me.app.MyView

Do you recommend any good android programming book?

damponting44

Reply
Vladimir S.
  • Forum posts: 266

May 17, 2016, 7:03:55 AM via Website

If your package name is "com.domain.me.app" then xml:

<com.domain.me.app.MyView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_View"
.............................
/>

Do you use Android Studio?

damponting44David Williams

Reply
David Williams
  • Forum posts: 14

May 17, 2016, 4:54:34 PM via Website

Yes I am using android studio.

When you allocate the custom view object in oncreate() does the variable name have to match something in the XML interface file?

damponting44

Reply
David Williams
  • Forum posts: 14

May 17, 2016, 5:53:39 PM via Website

With a closer look at it I think this may be the problem.
The sample custom View subclass may be for a different version of android?
It gives more detailed warnings about specific methods.
I'm compiling for android 22 (Kindle)
Maybe the sample custom view I loaded from android studio is for 23 or a newer version of android.

— modified on May 17, 2016, 5:54:22 PM

damponting44

Reply
David Williams
  • Forum posts: 14

May 17, 2016, 6:15:09 PM via Website

I'm getting the same error even when I compile for version 23 (on the emulator) unable to inflate the class, so I'm doing something wrong. I thought I was on to something.

— modified on May 17, 2016, 6:15:57 PM

Reply
David Williams
  • Forum posts: 14

May 17, 2016, 6:18:19 PM via Website

Should I try Open GL instead of the android drawing canvas? Any other options?
Maybe there is a preconfigured full screen drawing template?

Reply
Vladimir S.
  • Forum posts: 266

May 17, 2016, 8:28:54 PM via Website

Please, paste here logs with errors, maybe we can see where is a problem.
For the OpenGL you will need a customView also...

Reply
David Williams
  • Forum posts: 14

May 18, 2016, 11:57:42 PM via Website

That would be awesome if someone with more experience could take a look at this!

noisyguitar.com/warnings.txt
noisyguitar.com/compiler_errors.txt
noisyguitar.com/androidstudio.png

Reply
Vladimir S.
  • Forum posts: 266

May 19, 2016, 12:28:42 AM via Website

at com.noisyguitar.dmwilliams.appfour.MyView.invalidateTextPaintAndMeasurements(MyView.java:77)
at com.noisyguitar.dmwilliams.appfour.MyView.init(MyView.java:71)
at com.noisyguitar.dmwilliams.appfour.MyView.(MyView.java:33)

What have you placed in strings 33, 71 and 77 of MyView.java? Did you override onMeasure() method? Maybe this will help.

— modified on May 19, 2016, 12:33:22 AM

Reply
David Williams
  • Forum posts: 14

May 19, 2016, 2:22:18 AM via Website

Thanks!!!! All I had to do was set mExampleString = "my sample text" in thee init() function of the View subclass. Tricky google coders.

That was a lot of error messages!!

And make sure the font size is not set to 0, then linked to a non existent interface object!!!

— modified on May 19, 2016, 3:17:19 AM

Vladimir S.

Reply
David Williams
  • Forum posts: 14

May 19, 2016, 7:24:58 PM via Website

Lessoned learned: if there are a ton of errors. Search for the errors only in your code first.

Reply
Ashish Tripathi
  • Forum posts: 211

May 20, 2016, 2:33:49 PM via Website

better to share your code so can help.

Reply
ASIF MOSTAFA
  • Forum posts: 5

May 22, 2016, 12:22:06 PM via Website

well is your phone is rooted?

Reply
damponting44
  • Forum posts: 70

Oct 6, 2016, 2:35:00 PM via Website

With a more intensive take a gander at it I think this might be the issue.

The example custom View subclass might be for an alternate rendition of android?

It gives more itemized notices about particular techniques.

I'm aggregating for android 22 (Kindle)

Possibly the example custom perspective I stacked from android studio is for 23 or a more current variant of android.

Reply