所以我最近完成了我的应用程序的初始开发。我一直在使用我的手机(OnePlus 2)进行调试,一切都很好。 我决定从朋友和家人那里获得一些测试应用程序的帮助,有些人报告该应用程序在启动时崩溃了。所以我抓住了我母亲的电话(Galaxy S5)并决定记录错误。
logcat的
04-16 10:23:02.400 3229-3229/? I/Timeline: Timeline: Activity_launch_request id:com.test.drawernav time:3576923
04-16 10:23:02.730 3229-3229/? V/BitmapFactory: DecodeImagePath(decodeResourceStream3) : res/drawable-xxhdpi-v4/abc_ic_ab_back_mtrl_am_alpha.png
04-16 10:23:02.835 3229-3229/? V/BitmapFactory: DecodeImagePath(decodeResourceStream3) : res/drawable/bg.jpg
04-16 10:23:02.885 3229-3229/? I/art: Alloc sticky concurrent mark sweep GC freed 1308(219KB) AllocSpace objects, 5(80KB) LOS objects, 6% free, 118MB/126MB, paused 381us total 23.063ms
04-16 10:23:02.895 3229-3229/? I/art: Clamp target GC heap from 134MB to 128MB
04-16 10:23:02.895 3229-3229/? I/art: Alloc partial concurrent mark sweep GC freed 481(22KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 118MB/128MB, paused 346us total 9.467ms
04-16 10:23:02.915 3229-3229/? I/art: Clamp target GC heap from 134MB to 128MB
04-16 10:23:02.915 3229-3229/? I/art: Alloc concurrent mark sweep GC freed 58(14KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 118MB/128MB, paused 392us total 18.452ms
04-16 10:23:02.915 3229-3229/? I/art: Forcing collection of SoftReferences for 43MB allocation
04-16 10:23:02.935 3229-3229/? I/art: Clamp target GC heap from 134MB to 128MB
04-16 10:23:02.935 3229-3229/? I/art: Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 7% free, 118MB/128MB, paused 521us total 17.392ms
04-16 10:23:02.980 3229-3229/? E/art: Throwing OutOfMemoryError "Failed to allocate a 45619212 byte allocation with 9890668 free bytes and 9MB until OOM"
04-16 10:23:02.980 3229-3229/? D/skia: --- allocation failed for scaled bitmap
04-16 10:23:02.985 3229-3229/? D/AndroidRuntime: Shutting down VM
04-16 10:23:03.010 3229-3229/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.drawernav, PID: 3229
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.drawernav/com.test.drawernav.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v4.widget.DrawerLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v4.widget.DrawerLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.test.drawernav.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:614)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.test.drawernav.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 45619212 byte allocation with 9890668 free bytes and 9MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:741)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:562)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1014)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:3723)
at android.content.res.Resources.loadDrawable(Resources.java:3596)
at android.content.res.TypedArray.getDrawable(TypedArray.java:750)
at android.view.View.<init>(View.java:3939)
at android.view.ViewGroup.<init>(ViewGroup.java:511)
at android.view.ViewGroup.<init>(ViewGroup.java:507)
at android.support.v4.widget.DrawerLayout.<init>(DrawerLayout.java:351)
at android.support.v4.widget.DrawerLayout.<init>(DrawerLayout.java:347)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:614)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.test.drawernav.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
在Splash Screen(在每部手机上运行)显示5秒后调用主要活动。主要活动包括一个导航抽屉,整个应用程序围绕该抽屉设计。
MainActivity.java
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private ActionBarDrawerToggle drawerToggle;
private ActionBar actionBar;
private DrawerLayout drawerLayout;
private ListView navList;
private FragmentTransaction fragmentTransaction;
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navList = (ListView) findViewById(R.id.navigationdrawer);
ArrayList<String> navArray = new ArrayList<String>();
navArray.add("Home");
navArray.add("About Us");
navArray.add("Our Work");
navArray.add("Locate Us");
navArray.add("Contact Us");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1,
navArray);
navList.setAdapter(adapter);
navList.setOnItemClickListener(this);
navList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open_drawer, R.string.close_drawer);
drawerLayout.setDrawerListener(drawerToggle);
actionBar = getSupportActionBar();
assert getSupportActionBar() != null;
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
loadSelection(0);
}
private void loadSelection(int i){
navList.setItemChecked(i, true);
switch (i){
case 0:
HomeFragment homeFragment = new HomeFragment();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentholder, homeFragment)
.commit();
break;
case 1:
AboutUsFragment aboutUsFragment = new AboutUsFragment();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentholder, aboutUsFragment)
.commit();
break;
case 2:
HelplineFragment ourWorkFragment = new OurWorkFragment();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentholder, ourWorkFragment)
.commit();
break;
case 3:
LocateFragment locateFragment = new LocateFragment();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentholder, locateFragment)
.commit();
break;
case 4:
LocateFragment contactFragment = new ContactUsFragment();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentholder, contactFragment)
.commit();
break;
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if (drawerLayout.isDrawerOpen(navList)) {
drawerLayout.closeDrawer(navList);
} else {
drawerLayout.openDrawer(navList);
}
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
loadSelection(position);
drawerLayout.closeDrawer(navList);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
android:background="@drawable/bg"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentholder"> </FrameLayout>
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="@+id/navigationdrawer"
android:layout_gravity="start"
android:background="#ccddccdd"
></ListView>
</android.support.v4.widget.DrawerLayout>
HomeFragment.java
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class HomeFragment extends Fragment {
// ViewFlipper topFlipper;
ViewFlipper bottomFlipper;
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_home, container, false);
TextView textView = (TextView)view.findViewById(R.id.home_fragment_text);
bottomFlipper = (ViewFlipper)view.findViewById(R.id.bottomFlipper);
bottomFlipper.setAutoStart(true);
bottomFlipper.setFlipInterval(2000);
bottomFlipper.startFlipping();
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onDetach() {
super.onDetach();
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.drawernav.HomeFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/home_fragment_text"
android:text="@string/home_fragment_text"
android:textSize="27sp"
/>
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="2"
android:id="@+id/bottomFlipper">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottomFlipper1"
android:src="@drawable/flipper1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottomFlipper2"
android:src="@drawable/flipper2"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottomFlipper3"
android:src="@drawable/flipper3"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottomFlipper4"
android:src="@drawable/flipper4"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottomFlipper5"
android:src="@drawable/flipper5"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottomFlipper6"
android:src="@drawable/flipper6"/>
</ViewFlipper>
</LinearLayout>
</FrameLayout>
它与内存分配有关,这是我收集的,但我不知道如何解决它。
答案 0 :(得分:0)
无法开始活动 ComponentInfo {com.test.drawernav / com.test.drawernav.MainActivity}: android.view.InflateException:二进制XML文件行#2:
它是一个膨胀的例外, 您在res / drawable中引用的图像可能具有更高的像素分辨率。 它耗费了大量的内存。所以尽量减少src图像。