1

I just want to add photo.png as a back ground for activity when I added this photo I found this error how can I solve this Error

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 341712908 byte allocation with 3153834 free bytes and 91MB until OOM

and the error in this line  

at abtech.waiteriano.com.waitrer.MenuActivity.onCreate(MenuActivity.java:51)

  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)  this is my Activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/foods"
    tools:context="abtech.waiteriano.com.waitrer.MenuActivity">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:backgroundTint="#ff517d"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize" />

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        android:layout_width="match_parent"
        android:ems="8"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="search" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:prompt="@string/ru_prompt" />
    <!--   <FrameLayout
       android:id="@+id/content_frame"
       android:layout_width="matc
        h_parent"
       android:layout_height="wrap_content"></FrameLayout>-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:id="@+id/rl1"
        android:orientation="horizontal"
        android:weightSum="2">
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/bottom_panel">
        </FrameLayout>

        <LinearLayout
            android:id="@+id/bottom_panel"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:weightSum="2"
            android:layout_alignParentBottom="true">
            <RelativeLayout
                android:id="@+id/fireID"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="@drawable/background_border"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true">
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:src="@drawable/fire" />
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/cartID"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="@drawable/showorderbordersbackground"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true">
                <ImageView
                    android:id="@+id/imageCart"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:src="@drawable/shoppingcart" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

and this is my java class

package abtech.waiteriano.com.waitrer;

import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.SortedMap;

import abtech.waiteriano.com.waitrer.connection_class.ConnectionClass;
import abtech.waiteriano.com.waitrer.fragments.LVMenuFragment;
import abtech.waiteriano.com.waitrer.fragments.MenuGridFragment;
import abtech.waiteriano.com.waitrer.fragments.TablesFragment;

public class MenuActivity extends AppCompatActivity {
    private android.support.v7.widget.Toolbar toolbar;
    private ArrayList<String> Prests_ID;
    private ArrayList<String> Prests_Name;
    public static String Prst_ID = "0";
    public static long Check_ID = 0;
    public static int Check_Serial = 0;
    ImageView listIcon, gridIcon;
    boolean flag_item_grid = true;
    RelativeLayout fire, showOrderCart;
    private AutoCompleteTextView autoComplete;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        autoComplete = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
        Prests_ID = new ArrayList<String>();
        Prests_Name = new ArrayList<String>();
        Prests_ID.add("-1");
        Prests_Name.add("ALL");
        Prst_ID = ConnectionClass.Ret_Col("Select MainCourse From OutLet_Setup Where OutLet_ID = " + ConnectionClass.OutletID);
        if (Prst_ID.trim() == "")
            Prst_ID = "0";

        String str = "Select ID,Code,Name,Name2,(Select Count(Preset_ID) AS Cont From MenuItems Where MenuItems.Preset_ID = Presets.ID And MenuItems.Rest_ID_Active = 1 AND MenuItems.OutLet_ID_Active = 1 AND MenuItems.Item_ID <> 0) AS Items_Count From Presets Where Active = 1 And Rest_ID_Active = 1 AND OutLet_ID_Active = 1 ORDER BY Code";
        ResultSet rs = ConnectionClass.Ret_RS(str);
        try {
            while (rs.next()) {
                Prests_ID.add(rs.getString("ID"));
                Prests_Name.add(rs.getString("Name") + " (" + rs.getString("Items_Count") + ")");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        int indx = -1;
        indx = Prests_ID.indexOf(Prst_ID);

        Spinner menu_spinner = (Spinner) findViewById(R.id.spinner);
        menu_spinner.setAdapter(new MyCustomAdapter(MenuActivity.this,
                R.layout.spinner_row, Prests_Name));
        menu_spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
        if (indx != -1)
            menu_spinner.setSelection(indx);
        listIcon = (ImageView) findViewById(R.id.listicon);
        gridIcon = (ImageView) findViewById(R.id.gridicon);
        fire = (RelativeLayout) findViewById(R.id.fireID);
        showOrderCart = (RelativeLayout) findViewById(R.id.cartID);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Prests_Name);
        navigationSpinner.setAdapter(adapter);
        if (toolbar.getParent() != null)
            ((ViewGroup) toolbar.getParent()).removeView(menu_spinner);
        toolbar.addView(menu_spinner, 0);

        fire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = 0;
                String Ser = ConnectionClass.Ret_Col("Select Serial From ChecksItems WHERE (Check_ID = " + MenuActivity.Check_ID + ")");
                if (Ser.trim() != "")
                    i = Integer.parseInt(Ser);
                for (String key : TablesFragment.Check_Items.keySet()) {
                    SortedMap<String, String> sm1 = TablesFragment.Check_Items.get(key);
                    int qty = Integer.parseInt(sm1.get("Qty"));
                    if (qty > 0) {
                        i++;
                        double Price = Double.parseDouble(sm1.get("Price"));
                        Price = qty * Price;
                        String st = "insert into ChecksItems (Check_ID,Item_ID,QTY,UnitPrice,TotalPrice,DicountValue,Tax_Value,Adj_Value,NetPrice,Serial,Fired,Fired_Time,Voided,Voided_Time,Voided_Reason,P_On_Check,Complement,Status,IsModifier,Ref_Mod_Item,IsAssimbly,Ref_Ass_Item,Taxable,NoServiceCharge,Num_Fired,Num_Print,Server_ID,P_On_Report,Check_ID_Combine,Round_Check_Fired,Void_Effect_Invn,Promo_ID,Orig_Price,Officer,Comp_Reason_ID,End_Serial_Count,Discount_ID,Disc_Reason_ID,Hold,Hold_Time,Voided_By,Comp_By,Disc_By) values (" + String.valueOf(MenuActivity.Check_ID) + "," + sm1.get("Item_ID").toString() + "," + sm1.get("Qty") + "," + sm1.get("Price").toString() + "," + Price + ",0,0,0," + Price + "," + String.valueOf(i) + ",1,GetDate(),0,GetDate(),0,'" + sm1.get("PrintOnChick").toString() + "',0,'New',0,0,0,0,'" + sm1.get("Taxable").toString() + "','" + sm1.get("NoServiceCharge").toString() + "',0,0,0,'" + sm1.get("PrintOnReport").toString() + "',0,1,0,0," + sm1.get("Price").toString() + ",0,0,0,0,0,0,GetDate(),0,0,0)";
                        ConnectionClass.executeUpdate(st);
                    }
                }
                Toast.makeText(MenuActivity.this, "Fired", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
        showOrderCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MenuActivity.this, ShowOrderActivity.class);
                startActivity(i);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.iconmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item) {
        switch (item.getItemId()) {
            case R.id.searchicon:
                // ...
                return true;
            case R.id.listicon:
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.content_frame, new LVMenuFragment()).commit();
                flag_item_grid = false;
                return true;
            case R.id.gridicon:
                // ...
                fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.content_frame, new MenuGridFragment()).commit();
                flag_item_grid = true;
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public class MyCustomAdapter extends ArrayAdapter<String> {

        public MyCustomAdapter(Context context, int textViewResourceId,
                               ArrayList<String> objects) {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getDropDownView(int position, View convertView,
                                    ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView,
                                  ViewGroup parent) {
            // TODO Auto-generated method stub
            // return super.getView(position, convertView, parent);

            LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(R.layout.spinner_row, parent, false);
            TextView label = (TextView) row.findViewById(R.id.menu_items);
            label.setText(Prests_Name.get(position));
            return row;
        }
    }

    public class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view, int pos,
                                   long id) {
            Prst_ID = Prests_ID.get(pos);
            Toast.makeText(MenuActivity.this,
                    "you selected: " + Prests_ID.get(pos) + " " + Prests_Name.get(pos),
                    Toast.LENGTH_SHORT).show();
            if (flag_item_grid) {
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.content_frame, new MenuGridFragment()).commit();
            } else {
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.content_frame, new LVMenuFragment()).commit();
            }
//            Toast.makeText(
//                    parent.getContext(),
//                    "The Item is "
//                            + parent.getItemAtPosition(pos).toString(),
//                    Toast.LENGTH_LONG).show();
        }

        public void onNothingSelected(AdapterView parent) {
            // Do nothing.
        }
    }

}

this is my manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="abtech.waiteriano.com.waitrer">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".TabsActivity" />
        <activity
            android:name=".UseYourIp"
            android:screenOrientation="portrait" />
        <activity android:name=".MenuActivity" />
        <activity android:name=".ShowOrderActivity"></activity>
    </application>

</manifest>
4
  • Whats the size (resolution) of your background image ? Commented Mar 21, 2017 at 11:07
  • @Elvis Chidera Dimenstions 5656 * 3776 Commented Mar 21, 2017 at 11:09
  • 1
    @Dev.7arooney its too much heavy .Reduce size Commented Mar 21, 2017 at 11:10
  • It the first time to have this error :D thanks I think I know now how can I solve this Commented Mar 21, 2017 at 11:11

1 Answer 1

1

The resolution of your background image is very high (5656 * 3776). Resize it to something around 100* by 100* that should be better.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot it really very helpful
Your welcome. An alternative will be to use a library like glide or picasso to do the image loading but that might be too much work for the device. You could have two versions of the image too, one for phones which will be smaller and one for tablets which will be slightly bigger.
What resolution number could I use for tablets?
Well, that depends on you. But even for tablets, you shouldn't use a really high resolution. Use this link as a guide, it has the screen sizes of popular devices (phone & tablet): material.io/devices
thanks alot for you interests that's exactly wat I want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.