所以我在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_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/header">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/title"
android:layout_centerInParent="true"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/navigation_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
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)
答案 0 :(得分:1)
我认为您在ImageView
中设置的drawable太大了。请选择尺寸较小的图片以避免OutOfMemoryError
。
请检查评论中提到的@drawable/title
尺寸。
为了了解图像尺寸,我建议您阅读此beautiful article here。