DMan
DMan

Reputation: 1

Android Studio Activity keeps Crashing

Background: my activities are connected to each other through buttons (imagebuttons and buttons). they should launch a new activity every time they are clicked.

But my app keeps crashing after every 3 or so clicks. a message saying "Unfortunately THEAPP5 has topped". it will then relaunch that specific page at which point the button on that page will start working and connect to the next activity. however to only then repeat the issue on a different activity.

I have checked the button names/types/ID's etc. just can't figure out why this is happening. I'm a beginner with Android Studio.

I have attached the complete code from one activity. all activities have the same basic code.

package com.example.d.myluxuryapp5;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;

public class AutoLux extends Activity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_auto_lux);

    ImageButton AutoLuxury = (ImageButton) findViewById(R.id.luxuryUserBtn);

    AutoLuxury.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent(v.getContext(), LuxMM.class);
            startActivity(intent);
        }
    });
}}

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/luxury_background"
tools:context="com.example.d.myluxuryapp5.AutoLux">


<ImageButton
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/luxuryUserBtn"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100dp"
    android:background="@drawable/luxury_user_button"
    />

<ImageButton
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:id="@+id/otherUserBtn"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/other_user_button"
    />
</RelativeLayout>

Thanks for your help

LOGCAT for the moment the app crashes + relaunches

    03-07 10:43:34.081 3818-3818/com.example.d.myluxuryapp5 E/AndroidRuntime:FATAL EXCEPTION: main
    Process: com.example.d.myluxuryapp5, PID: 3818
    java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.example.d.myluxuryapp5/com.example.d.myluxuryapp5.OptOne}: android.view.InflateException: Binary XML file line #43: Binary XML file line #43: Error inflating class <unknown>
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: android.view.InflateException: Binary XML file line #43: Binary XML file line #43: Error inflating class <unknown>
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
    at android.app.Activity.setContentView(Activity.java:2166)
    at com.example.d.myluxuryapp5.OptOne.onCreate(OptOne.java:23)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: android.view.InflateException: Binary XML file line #43: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:645)
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
    at android.app.Activity.setContentView(Activity.java:2166) 
    at com.example.d.myluxuryapp5.OptOne.onCreate(OptOne.java:23) 
    at android.app.Activity.performCreate(Activity.java:6237) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at android.view.LayoutInflater.createView(LayoutInflater.java:619)
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
    at android.app.Activity.setContentView(Activity.java:2166) 
    at com.example.d.myluxuryapp5.OptOne.onCreate(OptOne.java:23) 
    at android.app.Activity.performCreate(Activity.java:6237) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: java.lang.OutOfMemoryError: Failed to allocate a 1172244 byte allocation with 197464 free bytes and 192KB until OOM
    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:1080)
    at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
    at android.content.res.Resources.loadDrawable(Resources.java:2540)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
    at android.view.View.<init>(View.java:3948)
    at android.widget.TextView.<init>(TextView.java:677)
    at android.widget.TextView.<init>(TextView.java:671)
    at android.widget.TextView.<init>(TextView.java:667)
    at java.lang.reflect.Constructor.newInstance(Native Method) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
    at android.app.Activity.setContentView(Activity.java:2166) 
    at com.example.d.myluxuryapp5.OptOne.onCreate(OptOne.java:23) 
    at android.app.Activity.performCreate(Activity.java:6237) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Upvotes: 0

Views: 4565

Answers (4)

Aaaad
Aaaad

Reputation: 1

I had exactly your problem I tried all above answers but didn't work , I got rid of problem by my self finally your problem is your new activity layout.xml (too maybe graphical) that`s why after 2or 3time it crashes have a good time.

Upvotes: 0

Carlos Carrizales
Carlos Carrizales

Reputation: 2340

EDITED:

  • I create the same project about what you post, and any exception, like yours, show up. May be some IDE problems, try to close it, open it and click on "clean project"

  • Also try Change it in your styles file

    ...

correct the intent, also add the fisnish(), this is to end the current activty but, if you want to go back from the LuxMM to AutoLux (or anyone), use another intent to navigate between them. Don't forget to add the permission to the manifest.

AutoLuxury.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent intent = new Intent(AutoLux.this, LuxMM.class);
            startActivity(intent);
            finish();
        }
    });

Upvotes: 0

Elshan
Elshan

Reputation: 7693

Use getApplicationContext() instead getContext()

AutoLuxury.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent intent = new Intent(getApplicationContext(), LuxMM.class);
            startActivity(intent);
        }
    });

Upvotes: 0

Use your activity instance instead of your view's one when creating the instance. Do it as follows:

Intent intent = new Intent(AutoLux.this, LuxMM.class);

Upvotes: 2

Related Questions