Android: Cannot inflate custom view in Robolectric test - android

So this is a possible duplicate of this question: Testing custom Views with Robolectric
But since that one doesn't have an accepted answer, I'm going to ask again, with my own code examples.
I'm using Robolectric 2.3 to run unit tests on a project which uses a custom view defined in a file called "here_now.xml" as:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<me.ambient.meetup.swiper.SwipeListView
xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:id="#+id/nearbyPeopleList"
android:listSelector="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
swipe:swipeFrontView="#+id/user_profile_layout"
swipe:swipeBackView="#+id/user_profile_bg"
swipe:swipeDrawableChecked="#drawable/plus"
swipe:swipeDrawableUnchecked="#drawable/minus"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeMode="both"/>
</LinearLayout>
That 'SwipeListView' is a custom view and it has those custom XML attributes. Now, this compiles and runs okay on the app. But when running Robolectric tests involving the parent activity that instantiates that view, I run into inflation errors:
me.ambient.meetup.test.HereNowActivityTest > testEditProfileMenuButton FAILED
android.view.InflateException: XML file /Users/shayak/Documents/SourceCode/MagnetAndroid/magnet-android/meetup/src/main/res/layout/here_now.xml line #-1 (sorry, not yet implemented): Error inflating class me.ambient.meetup.swiper.SwipeListView
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
at android.app.Activity.setContentView(Activity.java:1895)
at me.ambient.meetup.HereNowActivity.init(HereNowActivity.scala:93)
at me.ambient.meetup.HereNowActivity.onCreate(HereNowActivity.scala:34)
at android.app.Activity.performCreate(Activity.java:5133)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:113)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:265)
at org.robolectric.util.ActivityController.create(ActivityController.java:110)
at org.robolectric.util.ActivityController.create(ActivityController.java:120)
at me.ambient.meetup.test.HereNowActivityTest.testEditProfileMenuButton(HereNowActivityTest.scala:25)
Caused by:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createView(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:353)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at com.android.internal.policy.impl.PhoneWindow.$$robo$$PhoneWindow_1a87_setContentView(PhoneWindow.java:267)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
at android.app.Activity.$$robo$$Activity_c57b_setContentView(Activity.java:1895)
at android.app.Activity.setContentView(Activity.java)
at me.ambient.meetup.HereNowActivity.init(HereNowActivity.scala:93)
at me.ambient.meetup.HereNowActivity.onCreate(HereNowActivity.scala:34)
at android.app.Activity.$$robo$$Activity_c57b_performCreate(Activity.java:5133)
at android.app.Activity.performCreate(Activity.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
... 6 more
Caused by:
java.lang.RuntimeException: You forgot the attributes swipeFrontView or swipeBackView. You can add this attributes or use 'swipelist_frontview' and 'swipelist_backview' identifiers
at me.ambient.meetup.swiper.SwipeListView.init(SwipeListView.java:211)
at me.ambient.meetup.swiper.SwipeListView.<init>(SwipeListView.java:159)
... 36 more
I'll also put my Robolectric test here, which is in Scala FWIW:
#Config(manifest = "./src/main/AndroidManifest.xml")
#RunWith(classOf[RobolectricTestRunner])
class HereNowActivityTest extends Asserter with ThreadManipulator {
#Test
def testEditProfileMenuButton(): Unit = {
val activity: HereNowActivity = Robolectric.buildActivity(classOf[HereNowActivity]).create().start().get()
val editProfileItem = activity.findViewById(R.id.action_edit_profile).asInstanceOf[MenuItem]
activity.onOptionsItemSelected(editProfileItem)
val shadowActivity = Robolectric.shadowOf(activity)
val startedIntent = shadowActivity.getNextStartedActivity()
val shadowIntent = Robolectric.shadowOf(startedIntent)
assertEquals(shadowIntent.getComponent().getClassName(), "me.ambient.meetup.EditProfileActivity")
}
}
Does anyone know why I am unable to inflate a custom view when running Robolectric tests, as well as unable to set any of the custom attributes?

Related

Android.view.InflateException: Binary XML file line #25: Error inflating class ImageButton, Resource NotFoundException, XmlPullParserException

In API: 19 Iam facing this Inflate Exception but Iam using AppCompatDrawableManager which supports as vector drawables in API:19
three Exceptions
1. Error in Inflate Image button
2. Resources$NotFoundException File res/drawable/bckg_icon.xml from drawable resource
3. org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
I have tried to decerese the ImageButton drawable src which four formats(hdpi,mdpi,xxhdpi etc)
my Implementation :
xml File which is fragmnet xml layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/layoutPreviewActionBar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/previewFragment"
android:layout_width="fill_parent"
android:layout_height="#dimen/action_bar_height"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dp"
android:layout_marginLeft="10dp"
android:text="00:00"
android:id="#+id/timeduration"
android:textColor="#color/colorPrimary"/>
<ImageButton
android:id="#+id/localVideo"
android:layout_width="#dimen/icon_width"
android:layout_height="#dimen/icon_height"
android:layout_centerVertical="true"
android:layout_marginRight="14.1dp"
android:layout_toLeftOf="#+id/call"
android:background="#drawable/bckg_icon"
android:layout_margin="5dp"
android:src="#drawable/video_icon" />
<ImageButton
android:id="#+id/call"
android:layout_width="#dimen/icon_width"
android:layout_height="#dimen/icon_height"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/initiate_call_button"
android:clickable="true"
android:layout_margin="5dp"
app:srcCompat="#drawable/start_call" />
<ImageButton
android:id="#+id/localAudio"
android:layout_width="#dimen/icon_width"
android:layout_height="#dimen/icon_height"
android:layout_centerVertical="true"
android:layout_marginLeft="14.1dp"
android:layout_toRightOf="#+id/call"
android:background="#drawable/bckg_icon"
app:srcCompat="#drawable/mic_icon" />
</RelativeLayout>
My Fragment java class those drawables implementation :
rootView = inflater.inflate(R.layout.preview_actionbar_fragment, container, false);
mAudioBtn = (ImageButton)rootView.findViewById(R.id.localAudio);
mVideoBtn = (ImageButton)rootView.findViewById(R.id.localVideo);
mCallBtn = (ImageButton)rootView.findViewById(R.id.call);
timdura= (TextView)rootView.findViewById(R.id.timeduration);
c=mActivity;
booking_id=mActivity.bookingId;
drawableStartCall = AppCompatDrawableManager.get().getDrawable(mActivity,R.drawable.initiate_call_button);
drawableEndCall =AppCompatDrawableManager.get().getDrawable(mActivity,R.drawable.end_call_button);
drawableBckBtn = AppCompatDrawableManager.get().getDrawable(mActivity,R.drawable.bckg_icon);
My logCat details:
I/com. .MainActivity: OnAttach PreviewControlFragment
I/com.therightdoctors.dpmdoctor.MainActivity: OnCreate PreviewControlFragment
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41a22cf8)
W/dalvikvm: threadid=1: uncaught exception occurred
E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.therightdoctors.dpmdoctor/com.therightdoctors.dpmdoctor.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class ImageButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class ImageButton
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.therightdoctors.dpmdoctor.ui.PreviewControlFragment.onCreateView(PreviewControlFragment.java:165)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
at android.app.Activity.performStart(Activity.java:5353)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/bckg_icon.xml from drawable resource ID #0x7f02005c
at android.content.res.Resources.loadDrawable(Resources.java:2170)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3579)
at android.widget.ImageView.<init>(ImageView.java:123)
at android.widget.ImageButton.<init>(ImageButton.java:87)
at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:60)
at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:118)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:172)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
at com.therightdoctors.dpmdoctor.ui.PreviewControlFragment.onCreateView(PreviewControlFragment.java:165) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) 
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595) 
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758) 
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363) 
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149) 
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013) 
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) 
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607) 
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174) 
at android.app.Activity.performStart(Activity.java:5353) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: org.xmlpull.v1.XmlPullParserException: Binar
I have tried from StackOverflow some similar Question not working
to reduce the images sizes ,
2.use app:srcCompat instead of android:src attribute in XML
problem only in API:19 lower DPI devices how to Handle these type of Exceptions
I know it's been a while but I think that you are using a VectorDrawable as background of your ImageButton in here:
<ImageButton
android:id="#+id/localVideo"
android:layout_width="#dimen/icon_width"
android:layout_height="#dimen/icon_height"
android:layout_centerVertical="true"
android:layout_marginRight="14.1dp"
android:layout_toLeftOf="#+id/call"
android:layout_margin="5dp"
android:src="#drawable/video_icon"
android:background="#drawable/bckg_icon />" <!-- <- here is your problem -->
I figured out that using VectorDrawable as background of TextView, EditText, Button, ImageButton, etc using android:background="..." cause a crash on API 19 and before, Because android:background on older devices is using old APIs and when android tries to inflate your VectorDrawable it can not parse it and crashes. Note that when you are using app:srcCompat as the source image of your ImageView you are using Compat library and there is no problem.
For fixing this you can use one of 3 methods:
Use an ImageView behind your view and set its app:srcCompat to your VectorDrawable
Use a custom widget that loads VectorDrawable using Compat library in runtime and sets the background
Use alternative resources to set background of your View to your VectorDrawable in API 21 and above and set it to a BitmapDrawable (png file for example) in lower APIs.
Once I had faced same issue, You can try anyone of the following methods :
Use .png or .jpeg instead of xml, and make sure the drawables are copied to drawable folder ( sometime it copies to drawable(v21) folder and then these images will not be available for API below 21).
Check the same thing for .xml drawables, in which directory it is kept.(You can view directory structure by changing view from Android to Project
Use srcCompat instead of src in xml files. You may need to declare
compile 'com.android.support:support-vector-drawable:23.4.0'

AndroidRuntime: FatalException:

I really don't know what's happening. On Android Studio every thing is working fine, but when I try to run the app on my phone it gives this error.
AndroidRuntime: FatalException:
Process: com.example.vb.textreader, PID: 29524
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vb.textreader/com.example.vb.textreader.ocrcapture}: android.view.InflateException: Binary XML file line #10: Error inflating class com.example.vb.textreader.CameraSourcePreview
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2394)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5276)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.example.vb.textreader.CameraSourcePreview
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.vb.textreader.ocrcapture.onCreate(ocrcapture.java:67)
at android.app.Activity.performCreate(Activity.java:6041)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2285)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2394)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5276)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.vb.textreader.CameraSourcePreview" on path: DexPathList[[zip file "/data/app/com.example.vb.textreader-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.view.LayoutInflater.createView(LayoutInflater.java:575)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.vb.textreader.ocrcapture.onCreate(ocrcapture.java:67)
at android.app.Activity.performCreate(Activity.java:6041)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2285)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2394)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5276)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.example.vb.textreader.CameraSourcePreview" on path: DexPathList[[dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-support-annotations-23.4.0_932ddf769cdf6f7ff910db3bda1b13074bfa8aca-classes.dex", dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.example.vb.textreader/files/instant-run/dex/slice-internal_impl-23.4.0_ed28e3a48cbb17d2b91d4065fb1975ae0dafdc68-classes.dex", dex file "/data/data/com.example.vb.textreader/fil
I am trying to create an OCR app on android. There is no error in the code as viewed in Android Studio. Any help is greatly appreciated.
XML Code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
<com.example.vb.textreader.proprietary.CameraSourcePreview
android:id="#+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.vb.textreader.GraphicOverlay
android:id="#+id/graphicOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.example.vb.textreader.proprietary.CameraSourcePreview>
It seems that there is a custom view com.example.vb.textreader.CameraSourcePreview that's used in a XML layout file that's causing the problem.
Check it's attributes and implementation. if it depends on a library, check the dependencies in build.gradle.
It seems you are trying to use com.example.vb.textreader.CameraSourcePreview which must be a custom view you implemented. One thing that comes to my mind is to check to see if the custom view resides exactly at that location.
I'm not sure whether the provided solution is related to your issue but
try tocompile 'com.android.support:design:*****' instead of com.android.support:appcompat-v7:*****

At first install the app crashes, but after reinstall it works

I have never met so strange issue so far.
The app compiles fine. But at first run it crashes on the test device, but if the project is cleaned and re-run the app works fine.
If I try to install the apk the result is the same: installing the app crashes but after reinstalling it runs fine.
The issue is happening on all of my test devices and under Eclipss and Android Studio. I spent many hours to find a similar case but no avail.
The previous version of the app worked fine, but if I revert the files to an earlier version the issue remains.
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.plexnor.android.good.ViewfinderView" on path: DexPathList[[zip file "/data/app/com.plexnor.android.good-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.plexnor.android.good-1, /vendor/lib, /system/lib]]
My guess something went wrong with project set up but could not find the culprit. Deleting the bin folder, making countless project cleaning and restarts did not lead anywhere ...
The full stack trace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.plexnor.android.good/com.plexnor.android.good.CaptureActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class com.plexnor.android.good.ViewfinderView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2425)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:171)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1297)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5506)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class com.plexnor.android.good.ViewfinderView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:463)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:359)
at android.app.Activity.setContentView(Activity.java:2010)
at com.plexnor.android.good.CaptureActivity.onCreate(SourceFile:317)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
... 11 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.plexnor.android.good.ViewfinderView" on path: DexPathList[[zip file "/data/app/com.plexnor.android.good-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.plexnor.android.good-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.view.LayoutInflater.createView(LayoutInflater.java:565)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
... 22 more
android.view.InflateException: Binary XML file line #17: Error inflating class com.plexnor.android.good.ViewfinderView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:463)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:359)
at android.app.Activity.setContentView(Activity.java:2010)
at com.plexnor.android.good.CaptureActivity.onCreate(SourceFile:317)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:171)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1297)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5506)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.plexnor.android.good.ViewfinderView" on path: DexPathList[[zip file "/data/app/com.plexnor.android.good-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.plexnor.android.good-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.view.LayoutInflater.createView(LayoutInflater.java:565)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
... 22 more
java.lang.ClassNotFoundException: Didn't find class "com.plexnor.android.good.ViewfinderView" on path: DexPathList[[zip file "/data/app/com.plexnor.android.good-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.plexnor.android.good-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.view.LayoutInflater.createView(LayoutInflater.java:565)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:463)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:359)
at android.app.Activity.setContentView(Activity.java:2010)
at com.plexnor.android.good.CaptureActivity.onCreate(SourceFile:317)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:171)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1297)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5506)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
EDIT 1:
The app crashes earlier even entering the main activity when processing the layout.
The beginning of the layout:
<?xml version="1.0" encoding="UTF-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<SurfaceView
android:id="#+id/preview_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<RelativeLayout
android:id="#+id/relative_layout_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:layout_marginTop="0dp" >
<RelativeLayout
android:id="#+id/learning_swipe_relative_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:background="#android:color/transparent" >
<ImageView
android:id="#+id/learningSwipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="30dp"
android:src="#drawable/swipe" />
</RelativeLayout>
<com.plexnor.android.good.ViewfinderView
android:id="#+id/viewfinder_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/learning_relative_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent" >
Whats your logcat throws
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.plexnor.android.good.ViewfinderView"
ClassNotFoundException in Java is a subclass of java.lang.Exception and Comes when Java Virtual Machine tries to load a particular class and doesn't found the requested class in classpath.
Another important point about this Exception is that, It is a checked Exception and you need to provide explicitly Exception handling while using methods which can possibly throw classnotfoundexception in java either by using try-catch block or by using throws clause.
Solution
Check whether your classpath contains that jar, if your classpath
doesn't contain the jar then just add that class in your classpath.
Because I could not solve the issue I suspended that project for some months. Using Eclipse in my another project the same issue emerged after including a third party SDK to the project.
Cleaning project, changing things in build path did not help. So, I cloned the project to a new workspace and added the SDK ... and it worked that way. After trying out the old abandoned project it worked out of the box. Thus I think the issue caused by a bug in the development environment.

YouTubePlayer together with AppCompat v7 library

I have developed an app that uses YouTube android API to play a certain video inside the app. At that time I have used as target API 19, and also target API 19. Now I want do change the design of the app switching to Material Design using target API 22 and also using AppCompat-v7 :22+ library. But the YouTubeBaseActivity cant be opened and the application crashes. Below I will post what I have done so far.
EventDetail.java
public class EventDetail extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener { .
.
.
protected void onCreate(Bundle savedInstanceState) {
// remove the shadow from action bar
setTheme(R.style.Theme_TranslucentActionBar_ActionBar_NoOverlay);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_detail);
// enable back button
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// setting the action bar initially transparent
mActionBarBackgroundDrawable = new ColorDrawable(
Color.parseColor("#ec5c59"));
mActionBarBackgroundDrawable.setAlpha(0);
getSupportActionBar().setBackgroundDrawable(mActionBarBackgroundDrawable);
// binding the scrollview
...
Manifest File
<activity
android:name="com.dev.apk.evente.al.EventDetail"
android:icon="#drawable/ikona_projekte"
android:label="#string/title_activity_event_detail"
android:screenOrientation="portrait"
android:theme="#style/Theme.TranslucentActionBar.ActionBar.Overlay" />
Layut File:
<RelativeLayout
android:id="#+id/video_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:paddingLeft="23dp"
android:paddingRight="23dp"
android:paddingBottom="15dp"
android:background="#drawable/event_background_block" >
<TextView
android:id="#+id/video_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/white"
android:gravity="left"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="Video"
android:textColor="#color/VeryDarkGray"
android:textSize="22sp" >
</TextView>
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/video_label"
android:background="#color/white" />
</RelativeLayout>
Theme.xml File
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.TranslucentActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/Widget.ActionBar</item>
</style>
<style name="Theme.TranslucentActionBar.ActionBar" />
<style name="Theme.TranslucentActionBar.ActionBar.Overlay">
<item name="android:actionBarStyle">#style/Widget.ActionBar.Transparent</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="colorPrimary">#color/evente_date_color</item>
<item name="colorPrimaryDark">#color/evente_darken_color</item>
</style>
<!-- Theme without ActionBar shadow (inherits main theme) -->
<style name="Theme.TranslucentActionBar.ActionBar.NoOverlay" parent="Theme.TranslucentActionBar.ActionBar.Overlay">
<item name="android:windowContentOverlay">#null</item>
</style>
</resources>
So at the beginning it doesn't open because the method getActionBar() return Null, then i changed it to getSupportActionBar() but i doesn't recognized it. Then at the end I changed this line in the following:
public class EventDetail extends AppCompatActivity implements
YouTubePlayer.OnInitializedListener {
and it gives me the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myikub.al/com.dev.apk.evente.al.EventDetail}: android.view.InflateException: Binary XML file line #250: Error inflating class com.google.android.youtube.player.YouTubePlayerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$600(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #250: Error inflating class com.google.android.youtube.player.YouTubePlayerView
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.dev.apk.evente.al.EventDetail.onCreate(EventDetail.java:112)
at android.app.Activity.performCreate(Activity.java:5303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$600(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.dev.apk.evente.al.EventDetail.onCreate(EventDetail.java:112)
at android.app.Activity.performCreate(Activity.java:5303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$600(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
at com.google.android.youtube.player.YouTubePlayerView.<init>(Unknown Source)
....
I am using Android Studio. Any idea will be appreciated.
The problem is that you're using the YouTubePlayerView, which requires your Activity to extend YouTubeBaseActivity. Then you changed your activity, so that it now extends AppCompatActivity. This breaks YouTubePlayerView.
The solution is pretty simple: use the YouTubePlayerFragment class. This does not pose any requirement on the Activity, leaving you with plenty of options for your theming.
According to the stack trace the activity has to extend YouTubeBaseActivity when using the YouTube API
If you need to have an actionbar then I think the best route is to change the style to have no actionbar and just use a toolbar in your view.
paste this code inside gradle app
compile 'com.google.android.gms:play-services:8.4.0'
compile files('libs/YouTubeAndroidPlayerApi.jar')

Admob android studio, crashing app

I have put the admob .jar file in my project via project structure:
And I added code in for the admob banner into my layout files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:orientation="vertical" >
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="taken out for stack"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
<ListView
android:id="#+id/yourTasteStatistics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="0px"
android:divider="#null"
>
</ListView>
</LinearLayout>
But when I open a fragment with an ad on it, my app automatically crashes...Any ideas what step I must be missing here?
Here is the output:
05-13 17:56:44.053 12606-12606/com.beerportfolio.freeBP E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.beerportfolio.freeBP, PID: 12606
android.view.InflateException: Binary XML file line #14: Error inflating class com.google.ads.AdView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at com.beerportfolio.freeBP.BeerPage.onCreateView(BeerPage.java:30)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1097)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1895)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1514)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: DexPathList[[zip file "/data/app/com.beerportfolio.freeBP-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.beerportfolio.freeBP-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java)
at java.lang.ClassLoader.loadClass(ClassLoader.java)
at java.lang.ClassLoader.loadClass(ClassLoader.java)
at android.view.LayoutInflater.createView(LayoutInflater.java)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at com.beerportfolio.freeBP.BeerPage.onCreateView(BeerPage.java:30)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1097)
            at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1895)
            at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1514)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
            at android.os.Handler.handleCallback(Handler.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)
Line 30 is just where the fragments view is inflated. Make sense its crashing there if it does not like my admob ads.
Update:
As noted in the comments I was using out of date libs. I then followed this link for instructions on how to update to use google play services instead.
https://developer.android.com/google/play-services/setup.html?hl=pt-br
My problem is here on this step:
apply plugin: 'android'
...
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.google.android.gms:play-services:4.3.23'
}
When I add that to my grade the gradle fails to sync. My grade looks like this now:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
compile 'com.google.android.gms:play-services:4.3.23'
}
}
allprojects {
repositories {
mavenCentral()
}
}
And give me this error:
Error:(9, 0) Build script error, unsupported Gradle DSL method found: 'compile()'!
Possible causes could be:
- you are using Gradle version where the method is absent (Fix Gradle settings)
- you didn't apply Gradle plugin which provides the method (Apply Gradle plugin)
- or there is a mistake in a build script (Goto source)
Your dependencies block is in the wrong place. It looks like you edited build.gradle in your top level directory.
Open the one in your application module instead. There should be an android block there etc. Or just use the project structure dialog in android studio to edit it.

Categories

Resources