-4

I just recently started an android development class in school and am wondering why my code is not working? Any help would be greatly appreciated. I am creating a Tip Tracker app to help me keep track of my tips when delivering pizzas. I know a similar app was created, but I want to create my own, and it is also my Final Project for the semester. For now I'm just trying to navigate between windows to make sure I can link everything together before going too crazy on the UI. I don't see where I actually have a problem, but I'm getting the error "Unfortunately, [app] has stopped working" when clicking the "Delivery" button on my main screen. So my second window never actually appears. Also, my app is very slow and unresponsive sometimes. Thank you to anyone who can help. My code is as follows:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:gravity="center|bottom"
    android:orientation="vertical">

    <Button
        android:layout_width="155dp"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal|center"
        android:layout_marginBottom="10dp"
        android:background="@drawable/button_bg"
        android:text="Delivery"
        android:id="@+id/deliveryButton" />

    <Button
        android:layout_width="155dp"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal|center"
        android:layout_marginBottom="10dp"
        android:background="@drawable/button_bg"
        android:text="LOG"
        android:id="@+id/logButton" />

    <Button
        android:layout_width="155dp"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal|center"
        android:layout_marginBottom="10dp"
        android:background="@drawable/button_bg"
        android:text="MAPS"
        android:id="@+id/mapsButton" />

The code for my main java file is:

public class MainActivity extends AppCompatActivity {

private Button launchDeliveryActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    launchDeliveryActivity = (Button) findViewById(R.id.deliveryButton);

    launchDeliveryActivity.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            launchActivity();
        }
    });
}

private void launchActivity()
{
    Intent intent = new Intent(this, orderDetailsActivity.class);
    startActivity(intent);
}

And my second window is something I quickly threw together to make sure I would be able to navigate from the home screen to the second window and back:

<?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:background="@drawable/order_details"
    android:orientation="vertical">

    <Button
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:text="Home"
        android:id="@+id/homeScreenButton" />
</LinearLayout>

And finally, my second java file:

public class orderDetailsActivity extends AppCompatActivity {

private Button homeScreen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.order_details);

    homeScreen = (Button) findViewById(R.id.homeScreenButton);

    homeScreen.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            finish();
        }
    });
}

Logs are:

1:29 AM Executing tasks: [:app:assembleDebug]

1:29 AM Gradle build finished in 1s 781ms

1:31 AM Executing tasks: [:app:assembleDebug]

1:31 AM Gradle build finished in 7s 15ms

1:31 AM Instant Run performed a full build and install since the installation on the device does not match the local build on disk. (Don't show again)

1:40 AM Executing tasks: [:app:assembleDebug]

1:40 AM Gradle build finished in 1s 203ms

1:43 AM Executing tasks: [:app:assembleDebug]

1:43 AM Gradle build finished in 983ms

EDIT: The Logcat is:

03-27 03:37:59.550 31525-31525/com.example.boley.personaldeliveryassistant E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.boley.personaldeliveryassistant, PID: 31525
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.boley.personaldeliveryassistant/com.example.boley.personaldeliveryassistant.orderDetailsActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
    at android.app.ActivityThread.access$1100(ActivityThread.java:221)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
    at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at com.example.boley.personaldeliveryassistant.orderDetailsActivity.onCreate(orderDetailsActivity.java:15)
    at android.app.Activity.performCreate(Activity.java:6876)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:158) 
    at android.app.ActivityThread.main(ActivityThread.java:7224) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:657)
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:380) 
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
    at com.example.boley.personaldeliveryassistant.orderDetailsActivity.onCreate(orderDetailsActivity.java:15) 
    at android.app.Activity.performCreate(Activity.java:6876) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:158) 
    at android.app.ActivityThread.main(ActivityThread.java:7224) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at android.view.LayoutInflater.createView(LayoutInflater.java:631)
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:380) 
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
    at com.example.boley.personaldeliveryassistant.orderDetailsActivity.onCreate(orderDetailsActivity.java:15) 
    at android.app.Activity.performCreate(Activity.java:6876) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:158) 
    at android.app.ActivityThread.main(ActivityThread.java:7224) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
 Caused by: java.lang.OutOfMemoryError: Failed to allocate a 207355916 byte allocation with 16773008 free bytes and 53MB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)
    at android.content.res.Resources.loadDrawableForCookie(Resources.java:4215)
    at android.content.res.Resources.loadDrawable(Resources.java:4089)
    at android.content.res.Resources.loadDrawable(Resources.java:3939)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:886)
    at android.view.View.<init>(View.java:4230)
    at android.view.ViewGroup.<init>(ViewGroup.java:589)
    at android.widget.LinearLayout.<init>(LinearLayout.java:202)
    at android.widget.LinearLayout.<init>(LinearLayout.java:198)
    at android.widget.LinearLayout.<init>(LinearLayout.java:194)
    at java.lang.reflect.Constructor.newInstance(Native Method) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:631) 
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:380) 
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
    at com.example.boley.personaldeliveryassistant.orderDetailsActivity.onCreate(orderDetailsActivity.java:15) 
    at android.app.Activity.performCreate(Activity.java:6876) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:158) 
    at android.app.ActivityThread.main(ActivityThread.java:7224) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  
Gary Chen
  • 248
  • 2
  • 14
Anthony Boley
  • 107
  • 12
  • 1
    please add logs – bhaskar kurzekar Mar 27 '17 at 07:33
  • Please add the error !! – Ahmad MOUSSA Mar 27 '17 at 07:35
  • I'm not getting any errors in Android Studio, only when I run the application on my phone or on the emulator. The app loads very slowly, and when I click the "Delivery" button, which is the button that takes me to the second window, it says "Unfortunately, Personal Delivery Assistant has stopped." The second window is never displayed. – Anthony Boley Mar 27 '17 at 07:52
  • In Android Studio open the tab 'Android Monitor' and you will have the stacktrace there when it crashes. We need that to help you. It's gonna be red colored. – Adrian Coman Mar 27 '17 at 08:19
  • Remove the background image from OrderDetailActivity xml LinearLayout and check. i think you will getting out of memory because of large size of image. showing in log :- Caused by: java.lang.OutOfMemoryError: Failed to allocate a 207355916 byte allocation with 16773008 free bytes and 53MB until OOM – aj0822ArpitJoshi Mar 27 '17 at 09:25

2 Answers2

0

I think error are getting Caused by: java.lang.OutOfMemoryError: Failed to allocate a 207355916 byte allocation with 16773008 free bytes and 53MB showing in log so firstly remove image from order_details Linearlayout and check and if you want to use this image then reduce size and set runtime check this

Community
  • 1
  • 1
aj0822ArpitJoshi
  • 1,142
  • 1
  • 9
  • 25
0

Suspect of ANR is drawable "order_details". As there is no other component in your second activity. Please check the size of drawable "order_details". If it is huge then this might the reason for ANR. Try by changing the drawable or removing it.

Please check logs of device, it will have "I_am anr" tag. There might be some information in logs for anr.

Sonam
  • 572
  • 4
  • 13
  • Apparently both images I used for my backgrounds are too large. I took your advice and deleted the drawables from my xml files, resulting in the windows to just be a solid grey with buttons on them. Now it works flawlessly navigating between the windows. Thank you for the quick response! It just sucks, I spent a lot of time making those on Photoshop. I didn't change the size of the images because I wanted it to scale for different screen sizes, so made the images larger than I needed. Is there a way to still use the images if I crop them down or resize them? – Anthony Boley Mar 27 '17 at 09:22
  • Yes you can use them by reducing the size of the image by cropping or resizing it. Try to keep the size of the image in KB's. if you want to scale it according to the size of the device, it can be done programmatically. Check below link: https://developer.android.com/topic/performance/graphics/load-bitmap.html – Sonam Mar 27 '17 at 09:25