setContentView(R.layout.activity_main)的运行时错误
Posted
技术标签:
【中文标题】setContentView(R.layout.activity_main)的运行时错误【英文标题】:RunTime Error at setContentView(R.layout.activity_main) 【发布时间】:2018-10-02 12:08:39 【问题描述】:所以我的 android 应用程序出现异常,我不知道从何而来。是的,我的代码现在从试图修复它变得一团糟
MainActivity.kt
class MainActivity : AppCompatActivity()
var navigationDrawerIconsList: ArrayList<String> = arrayListOf()
var images_for_navdrawer= intArrayOf(R.drawable.navigation_allsongs,R.drawable.navigation_favorites,R.drawable.navigation_settings,R.drawable.navigation_aboutus)
object statified
var drawerLayout: DrawerLayout?=null
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
MainActivity.statified.drawerLayout= findViewById(R.id.drawer_layout)
navigationDrawerIconsList.add("All Songs")
navigationDrawerIconsList.add("Favourites")
navigationDrawerIconsList.add("Settings")
navigationDrawerIconsList.add("About Us")
val toggle = ActionBarDrawerToggle(this@MainActivity, MainActivity.statified.drawerLayout,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
MainActivity.statified.drawerLayout?.setDrawerListener(toggle)
toggle.syncState()
val mainScreenFragment = MainScreenFragment()
this.supportFragmentManager
.beginTransaction()
.add(R.id.details_fragment,mainScreenFragment,"MainScreenFragment")
.commit()
var _navigationAdapter= NavigationDrawerAdapter(navigationDrawerIconsList,images_for_navdrawer,this)
_navigationAdapter.notifyDataSetChanged()
var navigation_recycler_view = findViewById<RecyclerView>(R.id.navigation_recycler_view)
navigation_recycler_view.layoutManager = LinearLayoutManager(this)
navigation_recycler_view.itemAnimator=DefaultItemAnimator()
navigation_recycler_view.adapter = _navigationAdapter
navigation_recycler_view.setHasFixedSize(true)
override fun onStart()
super.onStart()
我的主要活动 XML 代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_
android:layout_
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_
android:layout_ />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_
android:layout_>
<RelativeLayout
android:layout_
android:layout_
android:id="@+id/header">
<ImageView
android:layout_
android:layout_
android:background="@drawable/bg_gradient"/>
<ImageView
android:layout_
android:layout_
android:background="@drawable/title"
android:layout_centerInParent="true"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/navigation_recycler_view"
android:layout_
android:layout_
android:layout_below="@+id/header"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
运行应用程序时出现以下错误 在使用设备测试应用程序时,我在 Android Studio 的 Logcat 中遇到错误。
04-22 15:11:07.104 22545-22545/com.example.geeth.magnitude E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
04-22 15:11:08.253 22545-22545/com.example.geeth.magnitude E/dalvikvm: Could not find class 'android.support.v4.widget.DrawerLayout$1', referenced from method android.support.v4.widget.DrawerLayout.<init>
04-22 15:11:08.259 22545-22545/com.example.geeth.magnitude E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.widget.DrawerLayout.onDraw
04-22 15:11:08.261 22545-22545/com.example.geeth.magnitude E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.widget.DrawerLayout.onMeasure
04-22 15:11:08.262 22545-22545/com.example.geeth.magnitude E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.widget.DrawerLayout.onMeasure
04-22 15:11:09.533 22545-22545/com.example.geeth.magnitude E/dalvikvm-heap: Out of memory on a 98010016-byte allocation.
04-22 15:11:09.740 22545-22545/com.example.geeth.magnitude E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.geeth.magnitude, PID: 22545
java.lang.OutOfMemoryError
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.loadDrawable(Resources.java:2166)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3596)
at android.widget.ImageView.<init>(ImageView.java:123)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:71)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:67)
at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:181)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:105)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092)
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.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.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.geeth.magnitude.activities.MainActivity.onCreate(MainActivity.kt:25)
at android.app.Activity.performCreate(Activity.java:5343)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5345)
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:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
请在问题中以文本形式发布您的 Java 堆栈跟踪。我们无法在该图像中看到完整的堆栈跟踪,其他开发人员也无法搜索您的图像。 你的@drawable/title
图片尺寸是多少?
您的可绘制资源太大。改用 Glide 或 Picasso 之类的库将它们加载到 Kotlin 代码中
问题已编辑@CommonsWare
@drawable/title 的大小为 3300x3300,为 png 格式 @Reaz Murshed
【参考方案1】:
我认为您在ImageView
中设置的drawable 太大。请选择尺寸较小的图像以避免OutOfMemoryError
。
请检查评论中提到的@drawable/title
大小。
要了解图像大小,我建议阅读此beautiful article here。
【讨论】:
你能告诉我图片的最小尺寸是多少作为标题@Reaz Murshed 请检查更新后的答案。一旦你了解了 dp,你就会明白你的图像的最大尺寸应该是多少。以上是关于setContentView(R.layout.activity_main)的运行时错误的主要内容,如果未能解决你的问题,请参考以下文章