我正在尝试创建一个Android应用程序。当我使用滚动视图时,应用程序崩溃了。布局中有十个按钮 我的XML代码如下。
04-04 05:23:57.483: E/AndroidRuntime(939): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.doctorlive/com.example.doctorlive.MainActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class <unknown>
04-04 05:23:57.483: E/AndroidRuntime(939): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.os.Looper.loop(Looper.java:137)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-04 05:23:57.483: E/AndroidRuntime(939): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 05:23:57.483: E/AndroidRuntime(939): at java.lang.reflect.Method.invoke(Method.java:525)
04-04 05:23:57.483: E/AndroidRuntime(939): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-04 05:23:57.483: E/AndroidRuntime(939): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-04 05:23:57.483: E/AndroidRuntime(939): at dalvik.system.NativeStart.main(Native Method)
04-04 05:23:57.483: E/AndroidRuntime(939): Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class <unknown>
04-04 05:23:57.483: E/AndroidRuntime(939): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
04-04 05:23:57.483: E/AndroidRuntime(939): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-04 05:23:57.483: E/AndroidRuntime(939): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.app.Activity.setContentView(Activity.java:1895)
04-04 05:23:57.483: E/AndroidRuntime(939): at com.example.doctorlive.MainActivity.onCreate(MainActivity.java:16)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.app.Activity.performCreate(Activity.java:5133)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-04 05:23:57.483: E/AndroidRuntime(939): ... 11 more
04-04 05:23:57.483: E/AndroidRuntime(939): Caused by: java.lang.reflect.InvocationTargetException
04-04 05:23:57.483: E/AndroidRuntime(939): at java.lang.reflect.Constructor.constructNative(Native Method)
04-04 05:23:57.483: E/AndroidRuntime(939): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
04-04 05:23:57.483: E/AndroidRuntime(939): ... 25 more
04-04 05:23:57.483: E/AndroidRuntime(939): Caused by: java.lang.OutOfMemoryError
04-04 05:23:57.483: E/AndroidRuntime(939): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.content.res.Resources.loadDrawable(Resources.java:2105)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.view.View.<init>(View.java:3364)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.widget.TextView.<init>(TextView.java:612)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.widget.Button.<init>(Button.java:107)
04-04 05:23:57.483: E/AndroidRuntime(939): at android.widget.Button.<init>(Button.java:103)
04-04 05:23:57.483: E/AndroidRuntime(939): ... 28 more
我需要这十个按钮才能进入我的布局。 谁能告诉我为什么会这样?提前致谢
以下是logcat报告
using
答案 0 :(得分:0)
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
at android.content.res.Resources.loadDrawable(Resources.java:2105)
at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
at android.view.View.<init>(View.java:3364)
at android.widget.TextView.<init>(TextView.java:612)
at android.widget.Button.<init>(Button.java:107)
at android.widget.Button.<init>(Button.java:103)
您因OutOfMemoryError
而崩溃。
您为Button
提供了非常大的背景信息。缩小它。
答案 1 :(得分:0)
从附加日志中,似乎是由OutOfMemoryError
导致的应用崩溃。
尝试将lower resolution image
用于Button
背景,或使用Glide
或Picasso
库加载大图片。
Picasso.with(context).load(R.drawable.YOUR_DRAWABLE_IMAGE).into(YOUR_VIEW);
或
Glide.with(context).load(R.drawable.YOUR_DRAWABLE_IMAGE).into(YOUR_VIEW);
参见文档: