2013-04-20 37 views
0

當我用按鈕打開一個新屏幕時,我的應用程序將不會響應並顯示ANR,因爲它耗費我的應用程序來加載頁面的時間太長,但我不'不知道如何解決我的問題。如何通過打開新屏幕來修復我的ANR

它的工作原理較舊的Android設備上,但我的設備的應用程序已經停止對:'(

,如果你需要更多的信息只是問;)

我是HTC One X運行在最新的Android 4.1.1的版本 我有一個應用程序,你可以免費聽和下載音樂,我有很多關於1佈局的意見超過80個意見是這個問題。

我可以給你的代碼,但所有的代碼都工作,因爲在Android 3.3.7一切工作正常在三星銀河迷你。問題是,我想我打開了新的屏幕的應用程序有更多的時間,因爲我已經讀了5秒鐘後自動ANR彈出

logcat的:

04-17 22:26:48.333: I/dalvikvm(20428): at dalvik.system.NativeStart.main(Native Method) 
04-17 22:26:48.333: E/dalvikvm(20428): Out of memory: Heap Size=65571KB, Allocated=62502KB, Limit=65536KB 
04-17 22:26:48.333: E/dalvikvm(20428): Extra info: Footprint=63651KB, Allowed Footprint=65571KB, Trimmed=0KB 
04-17 22:26:48.333: D/skia(20428): --- decoder->decode returned false 
04-17 22:26:48.333: W/dalvikvm(20428): threadid=1: thread exiting with uncaught exception (group=0x41b4e2d0) 
04-17 22:26:48.348: E/AndroidRuntime(20428): FATAL EXCEPTION: main 
04-17 22:26:48.348: E/AndroidRuntime(20428): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listentomusic/com.example.listentomusic.March}: android.view.InflateException: Binary XML file line #1209: Error inflating class <unknown> 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.app.ActivityThread.access$600(ActivityThread.java:151) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.os.Looper.loop(Looper.java:155) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.app.ActivityThread.main(ActivityThread.java:5485) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at java.lang.reflect.Method.invokeNative(Native Method) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at java.lang.reflect.Method.invoke(Method.java:511) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at dalvik.system.NativeStart.main(Native Method) 
04-17 22:26:48.348: E/AndroidRuntime(20428): Caused by: android.view.InflateException: Binary XML file line #1209: Error inflating class <unknown> 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.view.LayoutInflater.createView(LayoutInflater.java:613) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:365) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.app.Activity.setContentView(Activity.java:1912) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at com.example.listentomusic.March.onCreate(March.java:15) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.app.Activity.performCreate(Activity.java:5066) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) 
04-17 22:26:48.348: E/AndroidRuntime(20428): ... 11 more 
04-17 22:26:48.348: E/AndroidRuntime(20428): Caused by: java.lang.reflect.InvocationTargetException 
04-17 22:26:48.348: E/AndroidRuntime(20428): at java.lang.reflect.Constructor.constructNative(Native Method) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.view.LayoutInflater.createView(LayoutInflater.java:587) 
04-17 22:26:48.348: E/AndroidRuntime(20428): ... 26 more 
04-17 22:26:48.348: E/AndroidRuntime(20428): Caused by: java.lang.OutOfMemoryError: (Heap Size=65571KB, Allocated=62502KB) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:626) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:473) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.content.res.Resources.loadDrawable(Resources.java:1959) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.widget.ImageView.<init>(ImageView.java:120) 
04-17 22:26:48.348: E/AndroidRuntime(20428): at android.widget.ImageView.<init>(ImageView.java:110) 
04-17 22:26:48.348: E/AndroidRuntime(20428): ... 29 more 

xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@drawable/standaard_background" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/maart" 
     android:textSize="30sp" 
     android:textColor="#FFFFFF" 
     android:background="#000000" /> 

    <android.support.v7.widget.Space 
     android:id="@+id/space1" 
     android:layout_width="fill_parent" 
     android:layout_height="15dp" /> 

    <ListView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 
<!-- start -->    
<!-- song1 -->  
      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/s1" 
       android:background="#FFFFFF"/> 

      <ImageView 
       android:id="@+id/imageView1" 
       android:layout_width="match_parent" 
       android:layout_height="137dp" 
       android:contentDescription="@string/imageView1" 
       android:src="@drawable/a" /> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"> 

       <Button 
       android:text="@string/play" 
       android:id="@+id/btnsong1_1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"> 
       </Button> 

       <Button 
       android:text="@string/download" 
       android:id="@+id/btnsong1_2" 
       android:layout_toRightOf="@id/btnsong1_1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"> 
       </Button> 
      </LinearLayout> 

... 

<!-- song40 -->    
      <TextView 
       android:id="@+id/textView41" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/s40" 
       android:background="#FFFFFF"/> 

      <ImageView 
       android:id="@+id/imageView40" 
       android:layout_width="match_parent" 
       android:layout_height="137dp" 
       android:contentDescription="@string/imageView1" 
       android:src="@drawable/zn" /> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"> 

       <Button 
       android:text="@string/play" 
       android:id="@+id/btnsong40_1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"> 
       </Button> 

       <Button 
       android:text="@string/download" 
       android:id="@+id/btnsong40_2" 
       android:layout_toRightOf="@id/btnsong40_1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"> 
       </Button> 
      </LinearLayout> 

<!-- end --> 




     </LinearLayout> 

    </ListView> 

</LinearLayout> 

的.java:

package com.example.listentomusic; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 


public class Menu extends Activity { 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.menu); 


     Button btnmarch = (Button) findViewById(R.id.btnmarch); 
     btnmarch.setOnClickListener(new View.OnClickListener() { 


      public void onClick(View v) { 
      // TODO Auto-generated catch block 
      startActivity(new Intent("android.intent.action.MARCH")); 

       } 
     }); 
    } 
} 

Android清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.listentomusic" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="9" 
     android:targetSdkVersion="16" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.listentomusic.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".Menu" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MENU" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".About" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.ABOUT" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 


     <activity 
      android:name=".March" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MARCH" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 



    </application> 

</manifest> 
+0

你會得到一個 – 2013-04-20 15:17:13

+0

內存不足,以及如何我可以解決這個問題嗎? – ILikeAndroid 2013-04-20 15:18:17

+0

我想你必須嘗試在創建這個新活動之前釋放盡可能多的內存。一些想法從哪裏開始http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html – 2013-04-20 15:28:50

回答

0

超出內存:

這是需要解決的問題。

我對1佈局有很多意見80多個視圖是 問題。

嘗試減少視圖的數量並運行我認爲它會運行的問題是您在佈局中創建的大量視圖。這將因設備而異,因此某些設備可能支持您擁有的大量視圖。

我最近遇到了類似的問題的佈局是在大多數主要設備(三星,摩托羅拉,LG)的工作正常,但在崩潰的索尼XPERIA S。

+0

我修復了內存不足,但我不知道如何讓它在80個視圖上工作我不能減少它:) – ILikeAndroid 2013-04-20 16:00:25

+0

是的,你可以改變你的listview實現需要改變。閱讀這些http://www.vogella.com/articles/AndroidListView/article.html && http://www.androidhive.info/2011/10/android-listview-tutorial/ – 2013-04-20 17:07:17