DavidBalas
DavidBalas

Reputation: 333

Android app crashes on intent

My app is working fine, I have a main menu, but on the main menu when I hit "play" , my app crashing, here is the logcat when i'm pressing the button:

07-07 08:35:34.505: E/AndroidRuntime(4072): FATAL EXCEPTION: main
07-07 08:35:34.505: E/AndroidRuntime(4072): Process: com.example.theball, PID: 4072
07-07 08:35:34.505: E/AndroidRuntime(4072): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.theball/com.example.theball.MainActivity}: android.view.InflateException: Binary XML file line #30: Error inflating class com.android.internal.widget.ActionBarContainer
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.app.ActivityThread.access$800(ActivityThread.java:142)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.os.Looper.loop(Looper.java:136)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.app.ActivityThread.main(ActivityThread.java:5118)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at java.lang.reflect.Method.invokeNative(Native Method)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at java.lang.reflect.Method.invoke(Method.java:515)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at dalvik.system.NativeStart.main(Native Method)
07-07 08:35:34.505: E/AndroidRuntime(4072): Caused by: android.view.InflateException: Binary XML file line #30: Error inflating class com.android.internal.widget.ActionBarContainer
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3242)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at com.android.internal.policy.impl.PhoneWindowEx.generateLayout(PhoneWindowEx.java:20)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3305)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:334)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:328)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.app.Activity.setContentView(Activity.java:1993)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at com.example.theball.MainActivity.onCreate(MainActivity.java:44)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.app.Activity.performCreate(Activity.java:5275)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
07-07 08:35:34.505: E/AndroidRuntime(4072):     ... 11 more
07-07 08:35:34.505: E/AndroidRuntime(4072): Caused by: java.lang.reflect.InvocationTargetException
07-07 08:35:34.505: E/AndroidRuntime(4072):     at java.lang.reflect.Constructor.constructNative(Native Method)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
07-07 08:35:34.505: E/AndroidRuntime(4072):     ... 26 more
07-07 08:35:34.505: E/AndroidRuntime(4072): Caused by: java.lang.OutOfMemoryError
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.content.res.Resources.loadDrawable(Resources.java:2150)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.view.View.<init>(View.java:3565)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.view.ViewGroup.<init>(ViewGroup.java:470)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.widget.FrameLayout.<init>(FrameLayout.java:101)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at android.widget.FrameLayout.<init>(FrameLayout.java:97)
07-07 08:35:34.505: E/AndroidRuntime(4072):     at com.android.internal.widget.ActionBarContainer.<init>(ActionBarContainer.java:52)
07-07 08:35:34.505: E/AndroidRuntime(4072):     ... 29 more

Here is my MainActivity:

      package com.example.theball;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Display;
import android.widget.ImageView;

public class MainActivity extends Activity implements SensorEventListener {
        private SensorManager sensorManager;
        private Sensor accelerometer;
        @SuppressWarnings("unused")
        private long lastUpdate;
        AnimatedView animatedView = null;
        ShapeDrawable mDrawable = new ShapeDrawable();
        public static int x;
        public static int y;
        public static final int width = 50;
        public static final int height = 50;
        public boolean firstDraw = true;
        private int screen_width; 
        private int screen_height; 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // setContentView(R.layout.activity_main);
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            accelerometer = sensorManager
                    .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            lastUpdate = System.currentTimeMillis();
            animatedView = new AnimatedView(this);
            setContentView(animatedView);
        }
        @Override
        protected void onResume() {
            super.onResume();
            sensorManager.registerListener(this, accelerometer,
                    SensorManager.SENSOR_DELAY_GAME);
        }
        @Override
        protected void onPause() {
            super.onPause();
            sensorManager.unregisterListener(this);
        }
        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                x -= (int) event.values[0];
                y += (int) event.values[1];
                if(x <= 0 || x >= screen_width || y <= 0 || y >= screen_height) { 
                    Intent myIntent = new Intent(this, SplashScreen.class);
                    startActivity(myIntent);
                }
            }
        }
        public class AnimatedView extends ImageView { 
            Display display = getWindowManager().getDefaultDisplay(); 
            Point size = new Point(); 
            static final int width = 50; 
            static final int height = 50; 
            @SuppressLint("NewApi") 
            public AnimatedView(Context context) { 
            super(context); 
            // TODO Auto-generated constructor stub 
            display.getSize(size); 
            screen_width = size.x; 
            screen_height = size.y; 
            mDrawable = new ShapeDrawable(new OvalShape()); 
            mDrawable.getPaint().setColor(0xffffAC23); 
            mDrawable.setBounds(0, 0, screen_width, screen_height); 
            }

            @Override 
            protected void onDraw(Canvas canvas) { 
            mDrawable.setBounds(x, y, x + width, y + height); // left top right bottom 
            if(firstDraw) { 
            x = screen_width / 2; 
            y = screen_height / 2; 
            firstDraw = false; 
            } 
            mDrawable.draw(canvas); 
            invalidate(); 
            } 
            }
}

Here is my android manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.theball"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme" >
        <activity
            android:name="com.example.theball.SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.theball.MainActivity" />
        <activity android:name="com.example.theball.help"/>
    </application>
</manifest>

TY :) if you need something more just ask :)

Upvotes: 0

Views: 310

Answers (2)

Darish
Darish

Reputation: 11481

From the logcat it is clear the error is java.lang.OutOfMemoryError which can happen when you try to handle large image bitmaps.

This is the official doucumentation.

Upvotes: 1

SMR
SMR

Reputation: 6736

this is happening because of setContentView(animatedView); in the MainActivity.java.

It is causing OutOfMemoryError

So if you are using an emulator then increase your RAM size or otherwise try to reduce the size of the images etc you are using.

Upvotes: 3

Related Questions